-----------
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
