shira
January 26, 2009, 12:05pm
1
Hello,
let's say I have a text file:
word11 word12 word13
word21 word22 word23
word31 word32 word33
and I want to put the second field of each line into a list:
set list = `cut -d" " -f2 ${1}`
and I use space (" ") as a delimiter, only that there's a catch:
there can be more than one space between two words, for example:
word11 word12 word13
word21 word22 word23
word31 word32 word33
how do I ignore the multiple spaces, and make sure that:
list == {word12 word22 word32}
thanks,
Shira.
set list = `awk {print $2}' ${1}`
try awk
joeyg
January 26, 2009, 12:19pm
3
> echo "word1 word2 word3" | cut -d" " -f2
word2
> echo "word1 word2 word3" | cut -d" " -f2
> echo "word1 word2 word3" | tr -s " " | cut -d" " -f2
word2
> echo "word1 word2 word3" | awk '{print $2}'
word2
The tr -s will suppress repeated consecutive characters; thus eliminating the extra space characters.
Awk natively separates on space (one or more) or tab characters.
shira
January 26, 2009, 12:24pm
4
Thanks!
I was wondering if there's a solution that involves perhaps a pipeline or a loop instead of an awk?
I thought about
set list = `cat ${1} | cut -d" " -f3`
but it didn't work...
shira
January 26, 2009, 12:31pm
5
Hey Joey, thanks so much!
how do I write it (with tr) in my script?
like this?
set list = `tr -s " " | cut -d" " -f2 ${1}`
or like this:
set list = `cat ${1} | tr -s " " | cut -d" " -f2`
or in a different way?
And what is the tr function anyway? what does it do?
joeyg
January 26, 2009, 12:38pm
6
> cat file146
val1 val2 val3
val4 val5 val6
val7 val8 val9
> tr -s " " <file146 | cut -d" " -f2
val2
val5
val8
> tr -s " " <file146 | cut -d" " -f2 >file146.out
> cat file146.out
val2
val5
val8
OR....
> mylist=`tr -s " " <file146 | cut -d" " -f2`
> echo ${mylist}
val2 val5 val8
shira
January 26, 2009, 12:38pm
7
OK, I read about it, tr is a genial command!
It worked!
Joey, you're the best, thanks so much!
shira
January 26, 2009, 12:45pm
8
since tr can only read from the standard input (and I need to read from a text file), this is how I wrote it (and it worked):
set list = `cat ${1} | tr -s " " | cut -d" " -f2`
Thanks again so much.
joeyg
January 26, 2009, 12:49pm
9
mylist=`tr -s " " <file146 | cut -d" " -f2`
The <file146 accomplishes a similar task to a cat file146 command at the beginning.
Thus the following are equivalent:
mylist=`tr -s " " <file146 | cut -d" " -f2`
mylist=`cat file146 | tr -s " " | cut -d" " -f2`
Although, programmers prefer the first example.
shira:
since tr can only read from the standard input (and I need to read from a text file), this is how I wrote it (and it worked):
set list = `cat ${1} | tr -s " " | cut -d" " -f2`
Thanks again so much.
no need for the Useless Cat
set list = `tr -s ' ' < ${1} | cut -d' ' -f2`
shira
January 26, 2009, 12:59pm
11
Hello vgersh99,
so if I understand correctly, you redirect
${1} | cut -d' ' -f2
into
tr -s ' '
and then place it in the list.
But as I recall, each statement has to start with a command, but ${1} isn't a command.
I understand this works, but how does it work, actually?
shira:
Hello vgersh99,
so if I understand correctly, you redirect
${1} | cut -d' ' -f2
into
tr -s ' '
and then place it in the list.
But as I recall, each statement has to start with a command, but ${1} isn't a command.
I understand this works, but how does it work, actually?
No, that's incorrect.
tr -s ' ' < ${1} | cut -d' ' -f2
You make '${1}' to be a stdin input to "tr -s ' '".
The output of the this (tr) operation gets pipped as stdin input to 'cut'.
shira
January 26, 2009, 3:45pm
13
Oh, ok, now I got it.
Thanks.