Results 1 to 7 of 7
Hello there.
I have a script which reads lines from a file. I do this by setting variables in a for loop using cat. Now cat stops reading at a ...
- 03-17-2008 #1Just Joined!
- Join Date
- Oct 2006
- Posts
- 8
Ksh/bash: read file including delimiters ???
Hello there.
I have a script which reads lines from a file. I do this by setting variables in a for loop using cat. Now cat stops reading at a delimiting character, so the line:
This is my example
will set the variable four times. I would really like to put the whole line into one variable. This can probably be done with either awk or sed but I cannot seem to find how to do this.
Hope some of you can help me out.
Greetings ... Danny
- 03-17-2008 #2Linux Newbie
- Join Date
- Mar 2008
- Location
- Hyderabad
- Posts
- 109
You can use an array and later concatenate them
- 03-17-2008 #3Linux User
- Join Date
- Jun 2007
- Posts
- 318
The way I get a whole line in a variable is use the 'read' command as follows:
Use the -r option if lines will contain backslashes (\).Code:while read LNE do echo $LNE done < filename
- 03-19-2008 #4
- 03-19-2008 #5
cfa is correct.
IFS stands for "Internal Field Separator". It contains all of the characters on which input will be split. By default it contains spaces, tabs, and newlines (at least, there may be more).
If you want it to split on newlines, you might do:
Where 012 is the octal value for a newline.Code:IFS=$'\012' ...
DISTRO=Arch
Registered Linux User #388732
- 03-21-2008 #6Linux User
- Join Date
- Aug 2006
- Posts
- 458
- 03-21-2008 #7Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
Not relevant to the OP but the easiest way to get a whole file into a variable is:
Whitespace and newlines will be preserved, but lost if you echo the variable unquoted. To put a specific line in you could use sed:Code:VAR=$(<filename)
where LINE holds the number of the line you wish to extract.Code:VAR=$(sed -n ${LINE}p file)


Reply With Quote
