Vous êtes sur la page 1sur 16

ECOLE NATIONALE DES SCIENCES DE LINFORMATIQUE

AU. 2013-2014
Travaux pratiques
LES FILTRES SOUS UNIX
Z. Bouyahia
Groupe II1
6.

LES FILTRES SOUS


UNIX

awk '{print NR,$0}' in.txt


c=0; while read line; do ((c+=1));
echo $c $line; done < in.txt

Travail demand : Donner pour chaque


ensemble de commandes (quivalentes) le
rle et le rsultat attendu

7.
1.

8.

9.

10.

awk 'BEGIN{nl=0;nw=0}
{nl++;nw+=NF} END {print
"lines:",nl, "words:",nw}' in.txt

awk '{x=x+NF}END{print x}' in.txt


5.

cat in.txt | wc -w

awk '{print (total +=$1)}' in.txt


4.

awk '/^$/ {x += 1};END {print x }'


in.txt

awk '/./ {print}' in.txt | wc -l


grep "." in.txt | wc -l
3.

for i in `seq 1 15`;do echo


"$i";done

sed -n -e '$=' in.txt


awk 'END{print NR}' in.txt
awk '{n++} END{print n}' in.txt
2.

11.

var=`cat in.txt` ;
var="${var//[^i]/}" ; echo ${#var}

awk '/titi/{x+=1}END{print x}'


in.txt

-1/16-

awk '{print NR,$0}' in.txt


while read line; do N=$((N+1));
echo "Line $N = $line"; done <
in.txt

12.

19.

grep -n "pattern" in.txt


13.

grep -H "pattern" in.txt


20.

grep -c "pattern" in.txt


awk '/pattern/ {n++} END {print
n}' in.txt
14.

awk '{arr[$1]+=$2} END {for(i in


arr) {print i, arr[i]}}' in.txt |
sort
OPERATIONS SUR LES CHAMPS : NF

sed -n '/pattern/=' in.txt

21.

awk '{cnt=0 ; for(i=1; i<=NF; i++)


{if($i != "") {cnt++}} {print NR "
: "cnt" fields"}}' FS="," in.txt

15.

sed -n
'/pattern1/,/pattern2/{=;d;}'
in.txt

22.

16.

while read line;do echo


${line::5};done < in.txt
while read line ; do echo $line |
cut -c1-5 ; done < in.txt

nl in.txt
17.

23.

cat -n in.txt
sed = in.txt | sed 'N; s/\n/\t/'

colrm 1 5 < in.txt


awk 'sub("^.....", "")' in.txt
while read line ; do echo $line |
cut -c6- ; done < in.txt

18.

24.

-2/16-

sed 's/^.*\(.....\)$/\1/' in.txt


sed 's/\(.*\)\(.\{5\}\)/\2/'
in.txt
while read line;do echo ${line: 5};done < in.txt
awk '{print substr($0, length($0)
- 4, length($0) ) }' in.txt

awk '{print $(NF-1)}' in.txt


31.

25.

awk '{print NF}' in.txt


32.

awk 'sub(".....$", "")' in.txt


26.

awk '{print NF}' in.txt | sort -n


| sed -n '$p'
33.

echo "data1 line1" | sed 's/.* //'


echo "data1 line1" | sed -n 's/.*
//;p'
resultat: line1

awk '{print NF}' in.txt | sort -n


| sed -n '1p'

27.

34.

awk '{$NF=""; print $0}' in.txt


28.

awk -F'[ ]' '{print $2,$3}' in.txt


cut -d ' ' -f2,3 in.txt
35.

awk '{elapse = $1/3600;


if(elapse<8) print int($1/3600)}'
in.txt

awk 'NF<3' in.txt


29.

36.

awk '{print substr($1,2,3)}'


in.txt

sort -k1.22

30.

37.

-3/16-

while read line; do echo `expr


substr "$line" 2 6`; done < in.txt

awk 'length($1)>3 && length($2)<5


{print}' in.txt ........... #ET
awk 'length($1)>3 || length($2)<5
{print}' in.txt ........... #OU

43.

38.

while read line; do echo


"len($line)" | m4; done < in.txt
while read line; do echo `expr
length "$line"`; done < in.txt

awk '{if(length($1)<2 && $1~/2/)


{print $2} else {print $1}}'
in.txt

44.

OPERATIONS SUR LES LIGNES : NR - OCCURRENCES


cat in.txt | sed
"N;s/\(.*\)\n\(.*\)/\1 \2/"
cat in.txt | sed "N;s/\n/ /"
cat in.txt | sed '$ !N; s/\n/ /'

39.

45.

cat in.txt | cut -c1-4


for line in $(cat in.txt); do echo
`expr "$line" : '\(....\)'`; done
while read line; do echo `expr
"$line" : '\(....\)'`; done <
in.txt

cat in.txt | sed


"N;s/\(.*\)\n\(.*\)/\2 \1/"
46.

40.

sed '4,7d' in.txt

awk '{print substr($0, length($0)3, length($0))}' in.txt

47.

41.

sed '/toto/d' in.txt


grep -v "toto" in.txt

while read line; do echo


"substr($line,4)" | m4; done <
in.txt

48.

42.

-4/16-

sed '/^@/ {N;N;d;}' in.txt


49.

grep -m1 "pattern" in.txt


sed -n '/pattern/{p;q;}' in.txt
56.

sed -e '2,6 s/toto//g' in.txt


50.

sed -e '1,$ s/^.*=/new/' in.txt


57.

awk '/pattern/ {n++} END {print


"pattern ecrit" n "fois"}' in.txt
51.

sed
sed
sed
sed
awk
awk
awk

awk -F "pattern" '{print NF-1}'


in.txt
52.

'n;d' in.txt
-n 'p;n' in.txt
-n '1,${p;n;}' in.txt
'2~2d' in.txt
'FNR % 2' in.txt
'NR%2 {print}' in.txt
'NR%2 == 1' in.txt

58.

sed -e '1,$ s/t/b/1' in.txt


while read line; do echo
${line/t/b}; done < in.txt

sed
sed
sed
awk
awk
awk

53.

-n 'n;p' in.txt
-n '1,${n;p;}' in.txt
'1~2d' in.txt
'!(FNR % 2)' in.txt
'(NR+1)%2 {print}' in.txt
'NR%2 == 0' in.txt

59.

sed '0,/old/ s//new/2' in.txt


54.

sed -n '3,${p;n;n;n;n;}' in.txt


60.

sed 's/old/new/2' in.txt


sed '/old/ s//new/2' in.txt
awk '{print gensub(/old/, "new",
2)}' in.txt

sed -e '1~3d' in.txt

55.

-5/16-

awk '{f[NR]=$0} END {print f[10]}'


in.txt
awk 'NR == 10 {print}' in.txt

61.

67.

sed '0~3d' in.txt


sed 'n;n;d;' in.txt
62.

sed 10q in.txt


awk 'NR <=10{print}' in.txt
68.

sort -u in.txt
sort in.txt | uniq
awk '!x[$0]++' in.txt
awk '{ a[$1]++ } END {for (i in a)
imprime i}' in.txt | sort
sed '$!N; /^\(.*\)\n\1$/!P; D'
in.txt

sed '3,5!d' in.txt


sed -n '3,5p' in.txt
awk 'NR >= 3 && NR <= 5' in.txt
head -5 in.txt | tail -3
sed -n '3{:a;N;5!ba;p}' in.txt

63.

69.

cat in.txt | uniq -d


sed '$!N; s/^\(.*\)\n\1$/\1/; t;
D' in.txt

for i in fichiers*;do awk 'NR ==


5;NR == 10 {print $0}' $i;done

64.

70.

awk 'FNR==NR && a[$0]++ {b[$0] ;


next} $0 in b' in.txt in.txt

awk 'NR == 5,NR == 10 {print NR" "


$0}' in.txt

65.

71.

awk 'FNR==NR && a[$2]++ {b[$2] ;


next} $2 in b' in.txt in.txt

sed -n -e '1h; 1!p; 3{g;p}' in.txt


66.

72.

sed '10q;d' in.txt


sed '10!d' in.txt
sed -n '10p' in.txt

sed q in.txt

-6/16-

sed -n -e :a -e
'1,10!{P;N;D;};N;ba'

73.

79.

sed -n '$p' in.txt


sed '$!d' in.txt

sed '/^.\{6,\}/d' in.txt

74.

80.

sed '$!N;$!D' in.txt

sed -n '/^.\{6\}/p' in.txt

75.

81.

sed -e :a -e '$q;N;11,$D;ba'
in.txt

awk '{ if ( length > L ) {


L=length ; s=$0 } } END { print
L,"\""s"\""}' in.txt

76.

LES PATTERNS

head -1 in.txt ; tail -1 in.txt


sed -n '1p ; $p' in.txt
awk 'NR==1 ; END {print}' in.txt
sed q in.txt;sed '$!d' in.txt
sed q in.txt;sed '$\!d' in.txt
..........#selon version de Linux
IFS=$'\n';array=($(cat in.txt));
echo ${array[0]};sed '$!d' in.txt
........#en sous shell

82.

grep 'pattern' in.txt


grep -w 'pattern' in.txt
awk '/pattern/' in.txt
awk '$0 ~ /\ypattern\y/ {print}'
in.txt
sed -n '/pattern/p' in.txt
sed '/pattern/ !d' in.txt
grep '\<pattern\>' in.txt

77.

83.

sed 'N;$!P;$!D;$d' in.txt


78.

grep "pattern" in*.txt


for i in in*.txt;do seq=`ls
$i`;awk '/pattern/ {print seq,$0}'
seq=${seq} $i;done

sed -e :a -e '$d;N;2,10ba' -e
'P;D'

-7/16-

84.

awk '/pattern1/ {print $0}


/pattern2/ {print $0}' in.txt
awk 'FNR==NR && a[$0]=/^t/ ||
a[$0]=/^d/ {b[$0] ; next} $0 in b'
in.txt in.txt

sed -e '/./{H;$!d;}' -e
'x;/pattern/!d;' in.txt

90.

85.

sed '/[ab]/!d' in.txt


sed '/[ab]/\!d' in.txt

sed -e '/./{H;$!d;}' -e
'x;/pattern1/!d;/pattern2/!d'
in.txt

91.

86.

sed '/a/!d; /b/!d' in.txt


sed '/a/\!d; /b/\!d' in.txt

sed -e '/./{H;$!d;}' -e
'x;/pattern1/b' -e '/pattern2/b' e d in.txt

92.

87.

sed '0,/tata/ s//zaza/' in.txt

awk '$1 ~ /2/ {print $3}' in.txt


awk '$1 == "2" {print $3}' in.txt
awk '{if($1 ~ /t/){gsub(/t/,
"z",$1);print $0}}' in.txt
awk '$1 !~ /2/ {print $3}' in.txt
........... #syntaxe inverse
awk '$1 \!~ /2/ {print $3}' in.txt
...........#syntaxe inverse (selon
version Linux)

93.

sed -e '/titi/,/tutu/ {/toto/d}'


in.txt
94.

sed -n '/toto/,/tata/p' in.txt


sed -e '/toto/,/tata/ !{/./d}'
in.txt
sed -e '/toto/,/tata/ \!{/./d}'
in.txt
perl -e "while(<>) {print
if/toto/.../tata/}" in.txt
awk "/toto/,/tata/" in.txt

88.

awk '$1 !~ /pattern1/ && $2 !~


/pattern2/ ' in.txt
awk '$1 ~ /pattern1/ || $2 ~
/pattern2/ {next} {print}' in.txt
89.

-8/16-

95.

102.

sed -e '/pattern1/,/pattern2/d'
in.txt

sed -n '/pattern/,$p' in.txt

96.

103.

sed '/titi/,/tata/ s/toto/zz/g'


in.txt

sed -n '/'$var'/p' in.txt


104.

97.

grep -v "pattern" in.txt


awk '!/pattern/' in.txt
sed '/pattern/d' in.txt
sed -n '/pattern/!p' in.txt
awk '$0 ~ /pattern/ {next}
{print}' in.txt

sed '/toto/!s/t/z/g' in.txt


98.

105.

sed -n '/titi/{:a;N;/tutu/!ba;p;}'
in.txt
99.

sed -e '/pattern/ i\ligne crite


avant le pattern' in.txt
............ (option 'i' : avant
le pattern)
sed -e '/pattern/ a\ligne crite
aprs le pattern' in.txt
............ (option 'a' : aprs
le pattern)

sed -n '3,10{/titi/,/tutu/p}'
in.txt
100.

106.

sed '/pattern/,+2d' in.txt

sed -e '/pattern/ i\
............ (option
le pattern)
sed -e '/pattern/ a\
............ (option
le pattern)

101.

sed -e '3,/pattern/d' in.txt

-9/16-

' in.txt
'i' : avant
' in.txt
'a' : aprs

107.

awk '/^pattern/ {c=2; next} c-->0'


in.txt
awk 'BEGIN {counter=0};
$1=="pattern" {counter=2; next};
counter>0 {counter--; print}'
in.txt
108.

113.

sed '/tata/d' in.txt


sed -n '/tata/!p' in.txt
114.

sed -n '/tata/{g;1!p;};h' in.txt


115.

grep -A2 "pattern" in.txt


sed -n '/pattern/ {N;N;p;}' in.txt
109.

sed '/pattern/ {N;N;d;}' in.txt


110.

sed -n '/tata/{n;p;}' in.txt


116.

echo "a line1" | sed 's_\([az]\)\([ ]\)\([a-z]*\)\([09]\)_\1\2\3 \4_'

grep "." in.txt


awk "/./" in.txt
sed -n '/./ {p;d}' in.txt
111.

117.

sed -n '4,10
{/pattern1/,/pattern2/
{s/^0./y&/;/^$/d;s/m/w/g;p}}'
in.txt

sed '8,/fin/ s/toto/titi/g' in.txt


sed '/debut/,$ s/toto/titi/g'
in.txt
112.

118.

sed -e '/[0-9]/!d' in.txt


sed -e '/[0-9]/\!d' in.

sed
'/titi/,/tutu/{/titi/b;/tutu/b;s/.
*/SED/;}' in.txt

119.

-10/16-

awk '{ sub("[a-z]*$", ""}; print


}' in.txt
120.

cat in.txt | tr '[:alpha:]' +


121.

cat in.txt | tr '[:digit:]' +


122.

gensub(/t/,"z",2)}; NR!=4 {print}'


in.txt
126.

sed 'y/to/kl/' in.txt


127.

sed -e '4,10 s/t/&zorro/gw


out.txt' in.txt
128.

sed '/titi/ s//& et tata/g' in.txt


sed 's/titi/titi et tata/g' in.txt
129.

tr '\n' ' ' < in.txt


123.

sed -e '4s/t../zorro/g' in.txt

var="newpattern" ; awk '{gsub(


/oldpattern/, "'"$var"'" )};1'
in.txt
var="newpattern" ; awk -v v="$var"
'{gsub( /oldpattern/, v )}1'
in.txt

124.

echo 'abcdef' | sed 's/^../&Q/'


#resultat = .
echo 'abcdef' | sed 's/^../Q/'
#resultat = .
125.

130.

awk '{print gensub(/old/, "new",


2) }' in.txt
131.

sed -e '/toto/!s/o/zorro/' in.txt


132.

sed -e '4s/t/z/2' in.txt


awk 'NR==4 {print

-11/16-

139.

expr "$(cat in.txt)" :


"\(.*\)...$"
133.

sed '1,/^$/d' in.txt


140.

awk 'sub( "...$", "" )' in.txt


134.

while read line;do echo ${line: 5};done < in.txt


sed 's/^.*\(.....\)$/\1/' in.txt
sed 's/\(.*\)\(.\{5\}\)/\2/'
in.txt

sed -e '/tata/,/route/ {/^$/d}'


in.txt
141.

sed 's/\ \ */\ /g' in.txt

135.

sed 's/toto\|titi/tata/g' in.txt


136.

sed '/./!d' in.txt


sed -i '/^$/d' in.txt ...........
(l'option '-i' est a manier avec
precaution)
137.

142.

sed 's/^[ \t]*//;s/[ \t]*$//'


in.txt
143.

grep -v '^$' in.txt


grep '.' in.txt
sed -n '/^$/!p' in.txt
awk NF in.txt
awk '/./' in.txt
sed '/^$/d' in.txt
sed '/./!d' in.txt
awk '/^$/ {next} {print}' in.txt
144.

sed '/./,$!d' in.txt


138.

sed 's/^ *//g' in.txt

sed '/^$/q' in.txt

-12/16-

145.

sed 'n;n;G;' in.txt


151.

sed 's/ *$//g' in.txt


146.

sed '3!G' in.txt


152.

sed '/[0-9A-Za-z]/,/^$/{/^$/d}'
in.txt
147.

sed -e '/./!d' -e '$!N;s/\n/ /'


in.txt
sed -e '/./\!d' -e '$\!N;s/\n/ /'
in.txt
grep "." in.txt | sed '$\!N;s/\n/
/'
148.

sed -e '1i \debut\ du\ traitement'


in.txt
sed -e '3i \ajout\ a\ la\ 3eme\
ligne' in.txt
awk 'NR == 3 {print "line3"}1'
in.txt
sed -e '$a \fin\ du\ traitement'
in.txt
153.

sed
..
sed
..

sed -e '/./!b' -e '$!N;s/\n/ /'


in.txt
sed -e '/./\!b' -e '$\!N;s/\n/ /'
in.txt
grep "." in.txt | sed '$\!N;s/\n/
/' | sed G
INSERTIONS
149.

'/pattern/ i\ligne ecrite


pattern' in.txt
'/pattern/ a\ligne ecrite
pattern' in.txt

154.

sed -e '/pattern/ i\
............ (option
le pattern)
sed -e '/pattern/ a\
............ (option
le pattern)

' in.txt
'i' : avant
' in.txt
'a' : apres

155.

sed G in.txt
sed 'a\ ' in.txt
150.

-e
le
-e
le

sed -e '/pattern/ c\new line'


in.txt

-13/16-

156.

sed '/pattern1/,/pattern2/
c\**DELETED**' in.txt
157.

sed -e 'i\line before' in.txt


158.

sed -e '/pattern/ i\line before


pattern' in.txt
159.

sed '/tata/{x;p;x}' in.txt


sed -e '/pattern/ i\ ' in.txt
160.

sed '/tata/G' in.txt


sed -e '/pattern/ a\ ' in.txt
161.

sed '/tata/{x;p;x;G}' in.txt

163.

sed -e 's/^./ &/g' in.txt


sed '{s_^_ _}' in.txt
164.

sed -e 's/.$/& /g' in.txt


165.

sed -e '4s/t/& /g' in.txt


sed -e '4\!s/t/& /g' in.txt
166.

sed -r "2 ~ s/^(.{4})(.{3})/\2#/"


in.txt

DIVERS
167.

cat in.txt
sed '' in.txt
sed ':' in.txt
168.

sed 'p' in.txt

162.

sed '1r temp.txt' < in.txt


sed '/pattern/ r temp.txt' <
in.txt

169.

-14/16-

sed '{h;p;p}' in.txt


while read line;do for i in `seq 1
3`;do echo $line;done;done <
in.txt
170.

tac in.txt
sed -n '1!G;h;$p' in.txt
awk '{ a[i++]=$0 } END { for (j=i1; j>=0; ) print a[j--] }' in.txt

cat in.txt | xargs -n1 | sort |


uniq -c | sort -nr

177.

split -b 10 in.txt prefixe_out


178.

171.

cat in.txt | xargs -n1


awk '{i=1; while (i<=NF){print $i,
" ";i++}}' in.txt
172.

while read line; do echo -n


"$line" | dd cbs=1 conv=unblock
2>/dev/null; done < in.txt
173.

sed '{:a;$!N;s_\n_ _;ta}' in.txt


174.

sed '$!N; s/\n/ /g' in.txt


175.

awk '{printf "ligne%d: %s


",NR,$0>"z-cible"}' z-source

179. split -l 5 in.txt prefix_out


awk '{print >("prefix_out"
int((NR+4)/5))}' in.txt
180.

cat in.txt | sort -n -t" " -k2.4


181.

cat in.txt | sort -r -u


182.

cp old_file new_file
sed 'w new_file' old_file
183.

sed '/\,$/ {N; s_\,\n_ _}' in.txt


sed -e :a -e '/\,$/N; s_\,\n_ _;
ta' in.txt

176.

184.

-15/16-

sed -e :a -e '/,$/N ; s#\n## ; ta'


in.txt
185.

sed -e :a -e '$!N;s/\n=/ /;ta' -e


'P;D' in.txt
186.

echo -e "bonjour"|sed 's/./& /g'


echo -e "bonjour"|sed -r 's/([^
])/\1 /g'
echo -e "bonjour"|sed 's/\([^
]\)/\1 /g'
191.

echo -e "texte ligne1\ntexte


ligne2" | sed 's/^./\u&/'

awk '{key=$1;$1="";n=split($0,
parties, "[,]");for(k=1; k<=n;
k++) print ""key" "1" "parties[k]
""}' in.txt | awk '{if($2>0) print
$0}' | awk '{if($1 != key){key =
$1} else {$2 += cum} cum=$2;
print}' > out.txt

Bon travail

187.

awk '{key=$1;
$1=$2="";f[key]=f[key] s[key]
$0;s[key]=","} END {for(key in
f){gsub(/[[:space:]]/,"",f[key]);p
rintf "%s %s\n",key,f[key]}}'
in.txt| sort > out.txt
188.

sed -n "s/\([0-9][09]*\)/**\1**/p" in.txt


189.

sed -n '/[0-9]\{3\}/p' in.txt


190.

-16/16-