AWK and SED

I have a text file ("input.txt") with the following lines in it:

Code:
This is Line 1
This is Line 2
This is Line 3
THREE PEOPLE CAN KEEP A SECRET IF TWO ARE DEAD
This is Line 5
This is Line 6
This is Line 7

I have created the following two filters:

Code:
sed 's/^.*THREE //g;s/ IF.*$//g' < input.txt
awk '{sub(/.*THREE /,"");sub(/ IF.*/,"");print;}' < input.txt

I run these filters and receive the following output:

Code:
This is Line 1
This is Line 2 
This is Line 3 
PEOPLE CAN KEEP A SECRET
Tnis is Line 5
This is Line 6
This is Line 7

These scripts run perfectly on line 4. The rest of the lines...not so much. I do not want lines 1-3 and 5-7 to show up. I only want line 4 to be printed.

Can some gnarly ass ("King of all Stream Editors") help me out with this? If the two filters (above) can be written more efficiently, I wouldn't mind that either.

HELP!
 
The following sed command seems to give what you are looking for:

sed -n '/^.*THREE /{s///;s/ IF.*$//;p;}' < input.txt
Code:
PEOPLE CAN KEEP A SECRET

I have no experience with awk.
 
rolfheinrich said:
The following sed command seems to give what you are looking for:

sed -n '/^.*THREE /{s///;s/ IF.*$//;p;}' < input.txt
Code:
PEOPLE CAN KEEP A SECRET

I have no experience with awk.

Say I have the word "THREE" on line 4 (twice), how am I going to tell it which "THREE" to use? When you have duplicate words on the same line, the filter won't know which one to use. I think I need a lager now. This scripting makes me want to drink.:beer
 
rolfheinrich said:
The following sed command seems to give what you are looking for:

sed -n '/^.*THREE /{s///;s/ IF.*$//;p;}' < input.txt
Code:
PEOPLE CAN KEEP A SECRET

I have no experience with awk.

Say I want to add the word "This" on line 5, what would the syntax be?

Example:
Code:
PEOPLE CAN KEEP A SECRET This
 
Code:
This is Line 1
This is Line 2
This is Line 3
 THREE PEOPLE CAN KEEP THREE SECRETS IF TWO ARE DEAD
This is Line 5
This is Line 6
This is Line 7
$ sed -n '/THREE /{s///;s/ IF.*/ This/;p;}' < input.txt
Code:
 PEOPLE CAN KEEP THREE SECRETS This
$ sed -n '/^ *THREE /{s///;s/ IF.*/ This/;p;}' < input.txt
Code:
PEOPLE CAN KEEP THREE SECRETS This
 
Niatross said:
Say I have the word "THREE" on line 4 (twice), how am I going to tell it which "THREE" to use? When you have duplicate words on the same line, the filter won't know which one to use. I think I need a lager now. This scripting makes me want to drink.:beer
Adjust as needed:
Code:
$ echo three three three | sed 's/three//' | vis -l
 three three\$
$ echo three three three | sed 's/three//1' | vis -l
 three three\$
$ echo three three three | sed 's/three//2' | vis -l
three  three\$
$ echo three three three | sed 's/three//3' | vis -l
three three \$
$ echo three three three | sed 's/three//g' | vis -l
  \$
&quot said:
sed[/man]"]N - Make the substitution only for the N'th occurrence of the regular expression in the pattern space.

awk(1) doesn't have an exact functionality but the same is still possible with more code or using the third argument of sub() if you know the position.
 
Back
Top