Wednesday, October 22, 2008
Add text in middle of file - awk bash
Input file:
$ cat details.txt
f:30:12:20
f:40:43:26
f:50:52:25
n:12:78:90
n:13:32:89
n:13:78:12
s:45:23:89
s:90:23:12
Output required: It was required to add a line "-----------", in the middle of the above file, so that the output should look like this,
f:30:12:20
f:40:43:26
f:50:52:25
n:12:78:90
----------
n:13:32:89
n:13:78:12
s:45:23:89
s:90:23:12
The bash script using awk to insert a line in the middle of a file:
$ cat midins.sh
#!/bin/sh
FILE=$1
lines=$(awk 'END {print NR}' $FILE)
mid=$((lines / 2))
awk -v pos="$mid" '{
if(NR<=pos)
}' $FILE
echo "----------"
awk -v pos="$mid" '{
if(NR>pos)
}' $FILE
Executing:
$ ./midins.sh details.txt
f:30:12:20
f:40:43:26
f:50:52:25
n:12:78:90
----------
n:13:32:89
n:13:78:12
s:45:23:89
s:90:23:12
Similar post:
Add/Change/Insert lines to a file using sed
Labels:
Awk,
awk newbie,
Bash,
bash scripts,
bash shell,
bash shell newbie
Subscribe to:
Post Comments (Atom)
© Jadu Saikia www.UNIXCL.com
No comments:
Post a Comment