Friday, May 15, 2009

Append line identifier to each line - awk


This post is mainly for awk newbies.

Input file:

$ cat details.txt
area=55
172.22.23.5
172.22.23.6
172.22.23.7
area=54
172.16.1.96
172.16.1.97
172.16.1.98
area=60
172.21.5.12
172.21.5.13


Output required: Append the line identifier i.e. the "area id" to each of the related ips) i.e. required output:

172.22.23.5,55
172.22.23.6,55
172.22.23.7,55
172.16.1.96,54
172.16.1.97,54
172.16.1.98,54
172.21.5.12,60
172.21.5.13,60


Awk solutions:

$ awk -F "=" '
BEGIN { OFS="," }
{
if ($1 == "area") {
AID=$2
} else {
print $0,AID
}
}
' details.txt

or

$ awk -F "=" '
BEGIN { OFS="," }
/^area/ {AID=$2;next}
{print $0,AID}
' details.txt

or

$ awk -F "=" '
BEGIN { OFS="," }
$1=="area" {AID=$2;next}
{print $0,AID}
' details.txt

3 comments:

Nathan said...

And my non-awk way:

#!/bin/bash

declare -a array=(`cat txt`) # contains the input data
declare -i size=${#array[*]}
declare -i areaCode=0

for ((i=0; i<$size; i++))
do

if [[ ${array[$i]} =~ area ]]
then
areaCode=${array[$i]##*=}
else
array[$i]+=",${areaCode}"
echo "${array[$i]}"
fi

done

Unknown said...

@Nathan awesome, good use of bash array, thanks for the script.

Anirudh said...

sed -e '
/^area=/{h;d;}
G
s/\narea=/,/
'

© Jadu Saikia www.UNIXCL.com