0

I'm doing Unix shell scripting, I need write output to another file.

I have a log file like Shop.log,

Place=hyd
Location=Abids
Shop=Bigbazar
Place=Mumbai
Location=Worli
Shop=ShoppersStop
Place=Hyd
Location=Madhapur
Place=Mumbai
Location=Thane
Shop=Adidas
Place=hyd
Location=Abids
Place=Mumbai
Shop=ShoppersStop

In the above file, I need to check whether all 3 parameters are available or not, if not I need to assign with "N/A" value to that parameter and write into another final.txt. I tried with

bash-4.1$ pr -ats, /home/user/Shop.log --columns 3 > final.txt

but its giving some wrong result to me because of missing lines.

1 Answer 1

0

I do not think there is some command to easily modify the input file for this problem.

I haven't tested for all corner cases, this sed command helps when last line of input contains the 'Place' parameter

$ sed -i -E '$ s|^(Place.*)|\1\nShop=N/A|' /home/user/Shop.log

Then, using perl the missing parameters are added with 'N/A' value and output is passed on to pr

$ perl -e '@file = `cat abc.txt` ; @s = ("Place", "Location", "Shop");
> for($j=0;$j<=$#file;) {if($file[$j] !~ /^$s[$i]/){print "$s[$i]=N/A\n"}
> else {print $file[$j]; $j++} $i=($i+1)%3;}
> ' | pr -ats, --columns 3 > final.txt

$ cat final.txt
Place=hyd,Location=Abids,Shop=Bigbazar
Place=Mumbai,Location=Worli,Shop=ShoppersStop
Place=Hyd,Location=Madhapur,Shop=N/A
Place=Mumbai,Location=Thane,Shop=Adidas
Place=hyd,Location=Abids,Shop=N/A
Place=Mumbai,Location=N/A,Shop=ShoppersStop

Note that paste -d, - - - can also be used instead of pr -ats, --columns 3

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .