Tuesday, May 24, 2011

Bash insert newline after every 3 lines


-----------
Input file:
-----------
$ cat file.txt
FR 24
AA 33
EE 34
EE 46
BE 30
AA 31
DE 90
AL 10
AA 50
FR 67
EE 94
AA 80
NK 80

---------
Required:
---------
Insert a newline after every 3 lines of the above file. i.e. required output:
FR 24
AA 33
EE 34

EE 46
BE 30
AA 31

DE 90
AL 10
AA 50

FR 67
EE 94
AA 80

NK 80

----------
Solutions:
----------
a) Using awk:
$ awk '!( NR % 3 ) {$0 = $0"\n"} 1' file.txt

Which is same as:
$ awk '!( NR % 3 ) {$0 = $0"\n"} {print}' file.txt

b) A UNIX bash script to achieve this:
#!/bin/sh
c=0
while read line
do
((s=c%3))
if [ "$s" -eq 0 ]; then
echo -e "\n$line"
else
echo "$line"
fi
((c=c+1))
done < file.txt | sed '1d'

Following if-else block
 
if [ "$s" -eq 0 ]; then
echo -e "\n$line"
else
echo "$line"
fi

can also be written as:
[ "$s" -eq 0 ] && echo -e "\n$line" || echo "$line"

---------------------------
Any other alternative ?
---------------------------
Feel free to comment any other alternative to achieve this, much appreciated, thanks.

Related posts:
- UNIX Bash while loop sum issue explained
- Difference between awk NR and FNR

Sunday, May 8, 2011

Awk - replace n-th occurrence of pattern

Input file:
$ cat file.txt
FR 24
AA 33
EE 34
EE 46
BE 30
AA 31
DE 90
AL 10
AA 50
FR 67
EE 94
AA 80

Required:
Replace the "third" occurrence of first field "AA" with text "XX" in the above file.

Awk Solutions:
$ awk '$1=="AA" {
count++
if(count == 3){
sub("AA","XX",$1)
}
}
{print}' file.txt

Output:
FR 24
AA 33
EE 34
EE 46
BE 30
AA 31
DE 90
AL 10
XX 50
FR 67
EE 94
AA 80

In-case you want to add 100 to the 2nd field of that line where first field "AA" has occurred for the 3rd time, here is a way:
$ awk '$1=="AA" {
count++
if(count == 3){
$2=$2+100
}
}
{print}' file.txt

Output:
FR 24
AA 33
EE 34
EE 46
BE 30
AA 31
DE 90
AL 10
AA 150
FR 67
EE 94
AA 80

Related posts:
- Print up-to nth occurrence of a pattern in a file using awk
- Replace field other than the first occurrence using awk
- Awk print first occurrence of a set in a file

© Jadu Saikia http://unstableme.blogspot.com