LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-02-2010, 12:27 PM   #1
patolfo
Member
 
Registered: Jan 2006
Distribution: Debian-Sarge r2-k.2.6.8-2.386
Posts: 101
Blog Entries: 1

Rep: Reputation: 15
bash text to variable accessing individual text lines


Hi there guys, i am on processing text tasks
And i found that if you assign a text to a variable is chomp'ed automatically the newline
Code:
variable=$(cat file.txt)
The problem is i can only access the items/lines using:
Code:
for line in $variable
do
echo $line
# Other commands
done
Question is, how do i convert this to an indexed array. More importantly, how do i get access to individual $line[0], ..., $line[n]

Another thing, if the file.txt, has lines with spaces it is a mess using the for...in..., but echoing prints line by line...o_0
 
Old 05-02-2010, 01:18 PM   #2
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Try something like this:
Code:
i=0
while read line[$i]
do
  i=$(($i+1))
done < file.txt
See info bash for details re read and input redirection.
 
Old 05-02-2010, 10:51 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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:
Code:
OLD=$IFS
IFS=$'\n'
variable=($(cat file))

<for loop>
IFS=$OLD
 
Old 05-03-2010, 09:57 AM   #4
patolfo
Member
 
Registered: Jan 2006
Distribution: Debian-Sarge r2-k.2.6.8-2.386
Posts: 101

Original Poster
Blog Entries: 1

Rep: Reputation: 15
wow... IFS

Well i must admit i did not know about IFS

But it is helpful, to ask here
I will give ti a shot with IFS...

Thanks guys!
 
Old 05-03-2010, 10:22 AM   #5
patolfo
Member
 
Registered: Jan 2006
Distribution: Debian-Sarge r2-k.2.6.8-2.386
Posts: 101

Original Poster
Blog Entries: 1

Rep: Reputation: 15
text line in var, This is my output, any ideas

This is my code:
Code:
#!/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.

Last edited by patolfo; 05-03-2010 at 10:26 AM.
 
Old 05-03-2010, 10:41 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Close just a small change
Code:
OLD=$IFS
IFS=$'\n'
sipo=$(cat si_pregunta_por_mi)

<do your tests now>
 
Old 05-05-2010, 12:00 PM   #7
petrus4
LQ Newbie
 
Registered: May 2010
Posts: 9

Rep: Reputation: 1
Quote:
Originally Posted by patolfo View Post
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]
Code:
sed -n '1p' <file.txt>
The above will print one line. Also...
Code:
head -n5 <file.txt>
The above will print the first five lines, and
Code:
tail -n5 <file.txt>
this will print the last five.
 
Old 05-06-2010, 09:53 AM   #8
patolfo
Member
 
Registered: Jan 2006
Distribution: Debian-Sarge r2-k.2.6.8-2.386
Posts: 101

Original Poster
Blog Entries: 1

Rep: Reputation: 15
yes that is a way

the problem is that i want to print it like
Code:
echo ${line[5]}
and expecting the whole line,
this is accessing the text as an array basically, but thanks for your comment
 
Old 05-06-2010, 12:13 PM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
This should do the trick
Code:
#!/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
 
Old 05-10-2010, 04:46 PM   #10
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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).
 
Old 05-11-2010, 01:26 AM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by PTrenholme View Post
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).
Good point, thanks
 
Old 05-11-2010, 10:21 AM   #12
patolfo
Member
 
Registered: Jan 2006
Distribution: Debian-Sarge r2-k.2.6.8-2.386
Posts: 101

Original Poster
Blog Entries: 1

Rep: Reputation: 15
Thanks a lot Catkin, it finally worked
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] BASH - how to substitute many lines in a text file at once carolflb Linux - Newbie 6 10-20-2009 01:28 PM
[SOLVED] BASH - how to substitute many lines in a text file at once carolflb Programming 2 10-20-2009 10:21 AM
How to get variable from text file into Bash variable mcdef Linux - Software 2 06-10-2009 01:15 PM
bash- how to compare only certain lines of text files daberkow Linux - Newbie 2 06-01-2009 04:48 PM
bash script that can read lines of text palceksmuk Programming 1 12-25-2005 03:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration