dala
February 13, 2008, 2:44pm
1
I have a file containing the following lines:
first line
second line
third line
something goes here
something else goes here
something goes here
first line1
second line2
third line3
I need to go through the file and retrieved these lines and print them the output should look like this:
first line
second line
third line
first line1
second line2
third line3
Please help me get start it. I used while loop but the output wasn't what I want it.
otheus
February 13, 2008, 5:02pm
2
How do you know which lines you want and which you don't?
Let's say you want 3 lines, skip 3 lines, etc. Here's just one way (using GNU awk):
awk '(int((FNR-1)/ 3))%2 == 0' filename
dala
February 14, 2008, 9:38am
3
otheus:
How do you know which lines you want and which you don't?
Let's say you want 3 lines, skip 3 lines, etc. Here's just one way (using GNU awk):
awk '(int((FNR-1)/ 3))%2 == 0' filename
Thx otheus, to answer your question, I am using `grep "^first"`, when I found it I would continue and grep for the "^second" word and so on.
otheus
February 14, 2008, 9:53am
4
dala:
Thx otheus, to answer your question, I am using `grep "^first"`, when I found it I would continue and grep for the "^second" word and so on.
Okay, so you can use "egrep" for a more interesting regular expression:
egrep "^(first|second|third) " infile.txt
if that doesn't work, just use awk with almost identical syntax:
awk "/^(first|second|third) /" infile.txt
while read -r line
do
case $line in
first*|second*|third* ) echo "$line";;
esac
done < "file"
otheus
February 14, 2008, 10:20am
6
That looks like it should work, though like I said, the grep/awk solution would be more efficient. Maybe you should put double-quotes around the $line, as in:
case "$line" in
In ksh and bash, you can also parse out the first word from the rest:
case "${line%% *}" in
first|second|third) echo "$line";;
esac
otheus:
In ksh and bash, you can also parse out the first word from the rest:
case "${line%% *}" in
first|second|third) echo "$line";;
esac
while read -r a b
do
case $a in
....
esac
done
otheus
February 14, 2008, 9:42pm
8
Right you are, ghostdog! "Doh!" to me!