bash text to variable accessing individual text lines
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Above works fine, but you may also need to ask your self are you using the right tools, ie if you are traversing an entire file to make changes to certain lines
then maybe you should be using sed or awk or maybe even doing it in Perl.
Another alternative to yours which is reacting the IFS variable is to change it:
#!/bin/bash
sipo=$(cat si_pregunta_por_mi)
OLD=$IFS
IFS=$'\n'
#for line in $sipo
#do
#echo $line
#done
echo "$sipo[1]"
IFS=$OLD
exit
I tried to print only one line, can it be done?
Code:
Lorem ipsum
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Vivamus vel lectus arcu, sed lobortis odio.
Nunc tempor dolor tortor, nec consectetur massa.
Phasellus et eros non nisl posuere posuere at ac nibh.
Integer fermentum neque non libero sodales sodales.[1]
This is the input text:
Code:
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Vivamus vel lectus arcu, sed lobortis odio.
Nunc tempor dolor tortor, nec consectetur massa.
Phasellus et eros non nisl posuere posuere at ac nibh.
Integer fermentum neque non libero sodales sodales.
Lorem ipsum
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Vivamus vel lectus arcu, sed lobortis odio.
Nunc tempor dolor tortor, nec consectetur massa.
Phasellus et eros non nisl posuere posuere at ac nibh.
Integer fermentum neque non libero sodales sodales.[1]
#!/bin/bash
# Read file into array lines, one line per array element
while read line
do
lines+=( "$line" )
done < <(cat input.txt)
# See if it worked
for (( i=0; i< ${#lines[*]}; i++ ))
do
echo "${lines[i]}"
done
As I said in post #2, above, a simple done < input.txt does the trick.
The addition of the <(cat input.txt) is unnecessary unless you're using the cat command for additional input processing (which is not indicated in the proposed command).
As I said in post #2, above, a simple done < input.txt does the trick.
The addition of the <(cat input.txt) is unnecessary unless you're using the cat command for additional input processing (which is not indicated in the proposed command).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.