Computing.Net > Forums > Linux > Linux Shell scripting help

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Linux Shell scripting help

Reply to Message Icon

Name: shivaarun
Date: March 14, 2007 at 12:20:18 Pacific
OS: Linux
CPU/Ram: Linux
Comment:

Hi. I have two data files a.dat and b.dat.
a.dat contains :
s:b:
h:g:wq
and b.dat contains
c:b
d:b
f:b
w:r
My output should be :

s:b:c,d,f
h:g:wq,r.

Hpw can i accomplish this task in shell scripting


Regards
Arun



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: March 14, 2007 at 20:22:42 Pacific
Reply:

It's not pretty. Read a.dat and if it's line 1 grab fields 1, 2, 3 of file b and build the required string.

if it's line 2 of a.dat, grab the 2nd field of line 4 of b.dat, and build the string:

#!/bin/bash

cnt=0
while read line
do
((cnt+=1))
if [[ $cnt -eq 1 ]]
then
f1=$(awk ' BEGIN { FS=":" } { if (NR == 1) { print $1 ; exit } } ' b.dat)
f2=$(awk ' BEGIN { FS=":" } { if (NR == 2) { print $1 ; exit } } ' b.dat)
f3=$(awk ' BEGIN { FS=":" } { if (NR == 3) { print $1 ; exit } } ' b.dat)
line="${line},${f1},${f2},${f3}"
echo $line
fi

if [[ $cnt -eq 2 ]]
then
f4=$(awk ' BEGIN { FS=":" } { if (NR == 4) { print $2 ; exit } } ' b.dat)
line="${line},${f4}."
echo $line
fi

done < a.dat


0

Response Number 2
Name: shivaarun
Date: March 15, 2007 at 12:10:25 Pacific
Reply:

Thanks for the message. But what i have shown is only a sample. What i meant was my first file 2nd field matches with 2nd file 2nd field. Delimiter is ":" (Colon). My final output should be 1 st file 1st field,1st file 2nd field which is comman to both and third field should be 2nd file 1st field.

Regards
Arun


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Linux Forum Home


Sponsored links

Ads by Google


Results for: Linux Shell scripting help

begginner shell script help ASAP! www.computing.net/answers/linux/begginner-shell-script-help-asap/18683.html

linux bash shell script www.computing.net/answers/linux/linux-bash-shell-script/25054.html

advanced shell script help www.computing.net/answers/linux/advanced-shell-script-help/17749.html