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:
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
@Nathan awesome, good use of bash array, thanks for the script.
sed -e '
/^area=/{h;d;}
G
s/\narea=/,/
'
Post a Comment