Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Preventing taking several lines per variable

Status
Not open for further replies.

Iago77

IS-IT--Management
Jun 9, 2003
125
ES
I have a file with several lines.

This is an example:


021005 3683834834 odilon 08/10/07 07/09/17 07/09/17 index:0c8659e9-00000004-4252666f-4252666e-06a00000-ac100007
021005 2190666067 odilon 08/10/07 07/09/17 07/09/17 index:0c8659e9-00000004-4252666f-4252666e-06a00000-ac100007

I want to use a command with two arguments from the same line:

nsrmm -S <2nd Field> -w <4th field>

If I try:

for i in `cat /tmp/list.txt`
do
echo $i
done

The shell pass a cointain with spaces to the variable.

I decided to use IFS=<Control-V><Control-J>

But it takes two lines per variable.

How can I prevent taking more than a line per variable?

Thanks!
 
Try
Code:
IFS='
'

Ceci n'est pas une signature
Columb Healy
 
Alternatively, as long as ALL your lines have a similar format
Code:
while read on two three four five six seven
do
  nsrmm -S $two -w $four
done < /tmp/list.txt

Ceci n'est pas une signature
Columb Healy
 
What about this ?
while read f1 f2 f3 f4 x
do
echo nsrmm -S $f2 -w $f4
done < /tmp/list.txt

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Finally I got it:

1) Firstly, I set the appropiate IFS value:

IFS=$'\n'

2) Secondly, I developed this loop:

for i in `cat list.txt`
do
SSID=`echo $i | awk '{ print $2 }'`;
DATE=`echo $i | awk '{ print $1 }'`;
nsrmm -S $SSID -e $DATE
done

3) At last, it works as I wanted!

Thank you so much for your help!
 
I note that the IFS variable is being set to a newline in the post above but that the newline is delimited by single quotes and the quoted string is prefixed with a dollar.

Can anyone tell me what the dollar is for in this case?

==========================================
toff.jpg
I phoned the local ramblers club today, and this bloke just went on and on.
 
risby said:
I note that the IFS variable is being set to a newline in the post above but that the newline is delimited by single quotes and the quoted string is prefixed with a dollar.

Can anyone tell me what the dollar is for in this case?
generally '[red]$[/red]{[blue]var[/blue]}' references a shell variable (if the context of the discussion is 'shell scripting').
In this particular case I don't know what it means, but it looks like an indication that the OP doesn't know what (s)he is doing.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Hello,

at first reading I liked vgersh99's explanation. [wink]
But I couldn't understand how the OP's solution could work at all. [ponder]

And I found this:
man bash said:
Words of the form $'string' are treated specially. The
word expands to string, with backslash-escaped characters
replaced as specifed by the ANSI C standard. Backslash
escape sequences, if present, are decoded as follows:
...
\n new line
...
The expanded result is single-quoted, as if the dollar
sign had not been present.
So IFS=$'\n' is a correct way to change IFS to linefeed.
I would have done it the way columb suggested though. And I still prefer the solutions given by columb and PHV, because no changing of IFS is needed.

regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top