Command Output को Array में पढ़ना
In this page:
mapfile -t array_name < file_or_input
readarray -t array_name <<< "$data"
mapfile / readarray Basics
mapfile -t arr standard input को line by line पढ़ता है और हर line को arr के एक element के रूप में store करता है, -t हर line की trailing newline strip करते हुए। बिना manual looping के multi-line text को एक array में बदलने का यह सबसे clean तरीका है।
उदाहरण: mapfile / readarray Basics
#!/bin/bash
data=$'apple\nbanana\ncherry'
mapfile -t fruits <<< "$data"
echo "Element count: ${#fruits[@]}"
echo "Second fruit: ${fruits[1]}"
Login to try C/C++/Java/PHP code in the editor
readarray mapfile जैसा ही है
readarray बिल्कुल वही builtin है जो mapfile है, बस एक alternate नाम के under -- दोनों हर script में पूरी तरह interchangeable हैं। कुछ scripts stylistic reasons के लिए एक नाम उपयोग करते हैं, लेकिन कोई functional difference नहीं है।
उदाहरण: readarray Is the Same as mapfile
#!/bin/bash
printf "line one\nline two\nline three\n" > lines.txt
readarray -t lines < lines.txt
for line in "${lines[@]}"; do
echo "Read: $line"
done
Login to try C/C++/Java/PHP code in the editor
-t क्यों मायने रखता है
-t flag के बिना, mapfile/readarray हर captured element के end में trailing newline character attached रखता है, जो बाद में उन values को compare या display करते समय subtle bugs cause कर सकता है।
-t उपयोग करना इसे strip करता है, clean line content देते हुए।
उदाहरण: Why -t Matters
#!/bin/bash
data=$'first\nsecond'
mapfile -t clean_lines <<< "$data"
mapfile raw_lines <<< "$data"
echo "Clean (with -t): [${clean_lines[0]}]"
echo "Raw (without -t): [${raw_lines[0]}]"
Login to try C/C++/Java/PHP code in the editor
एक Single Line Split करने के लिए read -a
read -a arr input की एक line को IFS (default रूप से whitespace) के आधार पर array elements में split करता है, जो mapfile के काम से अलग है जो multiple lines को multiple elements में बदलता है।
यह एक space-separated line के fields parse करने के लिए handy है।
उदाहरण: read -a for Splitting a Single Line
#!/bin/bash
read -a fields <<< "red green blue"
echo "Number of fields: ${#fields[@]}"
echo "Second field: ${fields[1]}"
Login to try C/C++/Java/PHP code in the editor
arr=($(command))उपयोग करना और यह मान लेना कि हर line एक element बन जाता है; वह form असल में सभी whitespace पर word-split करता है (सिर्फ newlines पर नहीं), इसलिए multiple words वाली एक line multiple array elements में टूट जाती है।- यह भूल जाना कि
mapfile/readarray(वे same command हैं,readarrayएक alias है) Bash 4.0+ features हैं और बहुत पुराने Bash याshपर काम नहीं करेंगे। mapfile/readarrayके साथ-tउपयोग न करना, जो हर captured element के end में trailing newline character attached छोड़ देता है।
mapfile -t arr <<< "$data"(या< file) input को directly line by line एक array में पढ़ता है, एक line में एक element।readarrayसिर्फ बिल्कुल वही builtin का एक दूसरा नाम है जोmapfileहै -- वे पूरी तरह interchangeable हैं।-tflag हर line की store होने से पहले trailing newline strip करता है -- जो almost always वही है जो आप चाहते हैं।read -a arr <<< "$line"एक single line को whitespace से (IFSउपयोग करते हुए) एक array में split करता है, जो multiple lines पढ़ने से अलग use case है।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: