Computing.Net > Forums > Unix > column substitution

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.

column substitution

Reply to Message Icon

Name: sivakarthik
Date: September 28, 2005 at 09:32:20 Pacific
OS: Unix
CPU/Ram: 512
Comment:

hi,
I've 2 files, file1.txt and file2.txt

file1.txt has variables like
1111, 2222, 3333
4444,5555,6666
7777,8888,9999

file2.txt has values like
aaaa,bbbb,cccc
dddd,eeee,ffff,
gggg,hhhh,iiii

I wan to replace the columns and form a output file like

1111, bbbb, 3333
4444,eeee,6666
7777,hhhh,9999

Can you please help me.

Thanks

Hi,
I'm trying to replace a column in a comma seperated file with a column in another file using sed/awk. Can anyone help me.

Thanks
Siva




Sponsored Link
Ads by Google

Response Number 1
Name: Jim Boothe
Date: September 28, 2005 at 11:51:42 Pacific
Reply:

Here's one solution, and I am assuming that your two input files have the same number of lines and are sorted as desired.

The pr command will join the lines from each file side by side, then awk will print the desired columns. On some platforms, instead of pr -m (merge), it may be pr -j (join).

This code does not concern itself with spaces.  Columns are delimited by commas, and if you happen to have spaces in your data, they will remain.  Or awk can take those out with a gsub command.

pr -tm -s"," file?.txt|
  awk -F, '{OFS=",";print $1,$5,$3}'
1111,bbbb, 3333
4444,eeee,6666
7777,hhhh,9999


0

Response Number 2
Name: Jim Boothe
Date: September 28, 2005 at 13:43:36 Pacific
Reply:

I was using a wildcard on my testing. That first line should explicitly list your two input files:

pr -tm -s"," file1.txt file2.txt |


0

Response Number 3
Name: sivakarthik
Date: September 29, 2005 at 01:24:16 Pacific
Reply:

Great, pr -tm -s"," file1.txt file2.txt> file3 worked.
Thanks a lot Jim.

Siva

Hi,
I'm trying to replace a column in a comma seperated file with a column in another file using sed/awk. Can anyone help me.

Thanks
Siva



0

Response Number 4
Name: nails
Date: September 30, 2005 at 07:49:17 Pacific
Reply:

Another way is to place the second column of file 2 into an array, and read file 1 and replace the second column with what's in the array:

#!/bin/ksh

cnt=0
while IFS="," read c1 c2 c3
do
rr[$cnt]=$c2
cnt=$((cnt+1))
done < file2

cnt=0
while IFS="," read c1 c2 c3
do
printf "%s,%s,%s\n" "$c1" ${rr[$cnt]} "$c3"
cnt=$((cnt+1))
done < file1


0

Response Number 5
Name: sivakarthik
Date: September 30, 2005 at 08:22:30 Pacific
Reply:

Great, Thanks Nail.

Siva

Hi,
I'm trying to replace a column in a comma seperated file with a column in another file using sed/awk. Can anyone help me.

Thanks
Siva



0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: column substitution

Grabbing only the first column www.computing.net/answers/unix/grabbing-only-the-first-column/7510.html

replace Nth column www.computing.net/answers/unix/replace-nth-column-/7297.html

to calculate column total www.computing.net/answers/unix/to-calculate-column-total/3992.html