User Tools

Site Tools


cs_lang:awk

Simple examples

Compare two files

{
if (ancien == FILENAME)
	A[NR] = $0
else {
	if (ok == 1)  {
		NR = 1
		ok = 0 }
	B[NR] = $0 }
}
END {printf("%s", A[2])
	for(i=0 ; i <= NR ; i++) {
		for(j=1 ; j<length(A[i]) ; j++) {
			if (substr(A[i],j,1) != substr(B[i],j,1)) {
				printf("diff : %s\t%d\t%d",substr(A[i],j,1), i, j)
			}
		}
	}
}

Round time

ligne=""
{ for(i=1; i<=NF; i++) {
	if ($i !~ /^[0-2][0-9]:[0-5][0-9]:[0-5][0-9]$/)
		ligne=ligne" "$i
	else
	{
		split($i,T,":")
		T[3]=int((T[3]+5)/10)*10
		if (T[3]==60)
		{
			T[3]="00"
			T[2]++
			if (T[2]==60)
			{
				T[2]="00"
				T[1]++
			}
		}
		if (T[3] == 0) T[3] = "00"
		ligne=ligne" "T[1]":"T[2]":"T[3]
	}
}
print ligne
}

Replace (tr)

BEGIN {c1="i"; c2="I"}
{
for(i=1 ; i <= length($0) ; i++) {
	if (substr($0,i,1) == c1)
		printf("%c",c2)
	else
		printf("%c",substr($0,i,1)) }
printf("\n")
}

uniq

BEGIN { option = "" }
{ if ($0 == precedent)  i++
else { printf("\t%d\t%s\n", i, precedent)
	i=0 }
precedent = $0 }
cs_lang/awk.txt · Last modified: 2012/04/30 09:30 by cedric