# AWK and SED



## Niatross (Oct 17, 2013)

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


```
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:


```
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:


```
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!


----------



## Anonymous (Oct 17, 2013)

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

`sed -n '/^.*THREE /{s///;s/ IF.*$//;p;}' < input.txt`

```
PEOPLE CAN KEEP A SECRET
```

I have no experience with awk.


----------



## Juanitou (Oct 17, 2013)

This works too, but itâ€™s less clever:
`sed '/^This is Line/d ; s/^.*THREE //g ; s/ IF.*$//g' < input.txt`


----------



## Niatross (Oct 17, 2013)

rolfheinrich said:
			
		

> The following sed command seems to give what you are looking for:
> 
> `sed -n '/^.*THREE /{s///;s/ IF.*$//;p;}' < input.txt`
> 
> ...



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


----------



## Niatross (Oct 17, 2013)

rolfheinrich said:
			
		

> The following sed command seems to give what you are looking for:
> 
> `sed -n '/^.*THREE /{s///;s/ IF.*$//;p;}' < input.txt`
> 
> ...



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

Example:

```
PEOPLE CAN KEEP A SECRET This
```


----------



## Anonymous (Oct 17, 2013)

```
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`

```
PEOPLE CAN KEEP THREE SECRETS This
```
`$ sed -n '/^ *THREE /{s///;s/ IF.*/ This/;p;}' < input.txt`

```
PEOPLE CAN KEEP THREE SECRETS This
```


----------



## oops (Oct 17, 2013)

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:

```
$ 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.


----------

