awk :
1. To get selected fields from file
$ cat file
Name:No:E-mail
abc1:1:abc1@gmail.com
abc2:2:abc2@gmail.com
abc3:3:abc3@gmail.com
abc4:4:abc4gmail.com
$ cat file | awk -F: '{ print $1 " " print$2 }'
it prints Name and No
$cat file | awk -F: '{ print $3 }'
It prints All the E-mails
Foreach :
1. Print all the files
foreach i ( `ls` )
echo $i
end
2. Executive some script on all html files
foreach i ( `ls *.html` )
update_the_file.pl $1
end
sed:
1. Replace old_str to new_str in files
$ sed -i 's/old_str/new_str/g' test.txt
2. Replace in multiple files using shell
#!/bin/bash
for fl in *.html; do
mv $fl $fl.old
sed 's/FINDSTRING/REPLACESTRING/g' $fl.old > $fl
rm -f $fl.old
done
3. -i extension
Edit files in-place, saving backups with the specified extension.
sed:
1. Replace old_str to new_str in files
$ sed -i 's/old_str/new_str/g' test.txt
2. Replace in multiple files using shell
#!/bin/bash
for fl in *.html; do
mv $fl $fl.old
sed 's/FINDSTRING/REPLACESTRING/g' $fl.old > $fl
rm -f $fl.old
done
3. -i extension
Edit files in-place, saving backups with the specified extension.
e.g
$ ls
greetings.txt
$ cat greetings.txt
hello
hi there
$ sed -i .original 's/hello/bonjour/' greetings.txt
$ ls
greetings.txt
greetings.txt.bak
$ cat greetings.txt
bonjour
hi there
$ cat greetings.txt.original
hello
hi there
perl one-liner :
1. Replace old_str with new_str
$perl -0777 -e 's/old_str/new_str/g'
2. Replace multiple lines with multiple lines
$ perl -0777 -i.original -pe 's/a test\nPlease do not/not a test\nBe/igs' alpha.txt
$ diff alpha.txt alpha.txt.original
3. get the info same as awk
To get selected fields from file
$ cat file
Name:No:E-mail
abc1:1:abc1@gmail.com
abc2:2:abc2@gmail.com
abc3:3:abc3@gmail.com
abc4:4:abc4gmail.com
$ cat file | perl -F: -lane 'print F[0] " " F[1] '
it prints Name and No
$cat file | perl -F: -lane 'print F[2] '
It prints All the E-mails
perl one-liner :
1. Replace old_str with new_str
$perl -0777 -e 's/old_str/new_str/g'
2. Replace multiple lines with multiple lines
$ cat alpha.txt
This is
a test
Please do not
be alarmed
$ perl -0777 -i.original -pe 's/a test\nPlease do not/not a test\nBe/igs' alpha.txt
$ diff alpha.txt alpha.txt.original
2,3c2,3
< not a test
< Be
---
> a test
> Please do not
3. get the info same as awk
To get selected fields from file
$ cat file
Name:No:E-mail
abc1:1:abc1@gmail.com
abc2:2:abc2@gmail.com
abc3:3:abc3@gmail.com
abc4:4:abc4gmail.com
$ cat file | perl -F: -lane 'print F[0] " " F[1] '
it prints Name and No
$cat file | perl -F: -lane 'print F[2] '
It prints All the E-mails