How to use awk command in linux?

Here is the list of sed commands with examples

1. list content of file myfile
# [localhost@localhost ~] $ cat myfile
apple
orange
mango

banana
GUAVA
ra dish
pineapple

2. delete the first line of the file
# [localhost@localhost ~] $ sed '1d' myfile
orange
mango

banana
GUAVA
ra dish
pineapple

3. delete the third line of the file
# [localhost@localhost ~] $ sed '3d' myfile
apple
orange

banana
GUAVA
ra dish
pineapple

4. delete the last line of the file
# [localhost@localhost ~] $ sed '$d' myfile
apple
orange
mango

banana
GUAVA
ra dish

5. delete the 2nd and 4th line of the file
# [localhost@localhost ~] $ sed '2,4d' myfile
apple
banana
GUAVA
ra dish
pineapple

6. delete except the 2nd and 4th line of the file
# [localhost@localhost ~] $ sed '2,4!d' myfile
orange
mango

7. delete the 1st and last line of the file
# [localhost@localhost ~] $ sed '1d;$d' myfile
orange
mango

banana
GUAVA
ra dish

8. delete all lines beginning with character 'a'
# [localhost@localhost ~] $ sed '/^a/d' myfile
orange
mango

banana
GUAVA
ra dish
pineapple

9. delete all lines ending with character 'e'
# [localhost@localhost ~] $ sed '/e$/d' myfile
mango

banana
GUAVA
ra dish

10. delete all lines ending with either 'e' or 'E'
# [localhost@localhost ~] $ sed '/# [eE] $/d' myfile
mango

banana
GUAVA
ra dish

11. delete all the blank lines
# [localhost@localhost ~] $ sed '/^$/d' myfile
apple
orange
mango
banana
GUAVA
ra dish
pineapple

12. delete all lines which are entirely in UPPER CASE or CAPITAL LETTER
# [localhost@localhost ~] $ sed '/^# [A-Z]*$/d' myfile
apple
orange
mango
banana
ra dish
pineapple

13. delete all lines containing the pattern 'an'
# [localhost@localhost ~] $ sed '/an/d' myfile
apple

GUAVA
ra dish
pineapple

14. delete all lines not containing the pattern 'an'
# [localhost@localhost ~] $ sed '/an/!d' myfile
orange
mango
banana

15. delete all lines containing the pattern 'an' or 'le'
# [localhost@localhost ~] $ sed '/an\|le/d' myfile

GUAVA
ra dish

16. delete lines starting from 1st until meeting the PATTERN 'banana'
# [localhost@localhost ~] $ sed '1,/banana/d' myfile
GUAVA
ra dish
pineapple

17. delete lines meeting the PATTERN 'banana' till the LAST line
# [localhost@localhost ~] $ sed '/banana/,$d' myfile
apple
orange
mango

18. delete the last line only if it contains the PATTERN 'apple'
# [localhost@localhost ~] $ sed '${/apple/d;}' myfile
apple
orange
mango

banana
GUAVA
ra dish

No comments: