LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-19-2010, 11:30 AM   #1
neo2k
LQ Newbie
 
Registered: Aug 2008
Posts: 21

Rep: Reputation: 15
Column manipulation in bash


Hi guys,

I have a text file and i need to replace the 3rd column of that file from row 3 to the end of the file with a column which i have stored in the different text file.

For e.g the original file is like given below

a.txt nobla 6 gadf 72.500 1.600 1.800 .850 5.250 8.540
A# rad ang ht prf bk sd dia type blade
1 0.3081 9.00 1.9235 -17.50 18.00 -3.00 0.6250 1613 1
2 0.6509 194.00 2.0316 -17.50 18.00 -3.00 0.6250 1613 4
3 1.0128 8.00 2.1457 -17.50 18.00 -3.00 0.6250 1616 1
4 1.3748 192.00 2.2598 -17.50 18.00 -3.00 0.6250 1616 4
5 1.6986 7.00 2.3619 -17.50 18.00 -3.00 0.6250 1616 1
6 1.9347 120.00 2.4364 -17.51 18.00 -3.00 0.6250 1616 5
7 2.1327 190.00 2.4988 -17.34 18.00 -3.00 0.6250 1616 4
so lets say i wanna replace column 3 from row 3 to row 7 with a data from another file which is given below
54.00
239.00
53.00
237.00
52.00
165.00
235.0
so the final output file should be like this
a.txt nobla 6 gadf 72.500 1.600 1.800 .850 5.250 8.540
A# rad ang ht prf bk sd dia type blade
1 0.3081 54.00 1.9235 -17.50 18.00 -3.00 0.6250 1613 1
2 0.6509 239.00 2.0316 -17.50 18.00 -3.00 0.6250 1613 4
3 1.0128 53.00 2.1457 -17.50 18.00 -3.00 0.6250 1616 1
4 1.3748 237.00 2.2598 -17.50 18.00 -3.00 0.6250 1616 4
5 1.6986 52.00 2.3619 -17.50 18.00 -3.00 0.6250 1616 1
6 1.9347 165.00 2.4364 -17.51 18.00 -3.00 0.6250 1616 5
7 2.1327 235.00 2.4988 -17.34 18.00 -3.00 0.6250 1616 4

And i will post the code whatever i have tried soon..i started with awk and cut commands..but never got it to work

and also tried PASTE command too.

Thanks for your help..all i need is a steer in the right direction.

Thanks
Neo
 
Old 03-19-2010, 12:11 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
What have you tried with awk? It can be done by retrieving additional input (from the column file) using "getline from a file into a variable" (see section 3.8.4 of the GNU awk User's Guide for details). Example:
Code:
awk 'NR > 2 {getline $3 < "column"} 1' file
where "column" is the name of the file containing the replacements.
 
1 members found this post helpful.
Old 03-19-2010, 12:20 PM   #3
mario.almeida
Member
 
Registered: May 2008
Location: India
Distribution: Ubuntu 10.04, CentOS
Posts: 179

Rep: Reputation: 27
Hi,

use the below code

Code:
#!/bin/sh

i=1
file1=original_file
file2=secondfile
for x in `sed '1,2d' $file1 | sed 's/ /,/g'`
do
        y=`echo $x | awk -F, '{print $1" "$2}'`
        z=`sed "${i}!d" $file2`
        echo $y $z
       (( i++ ))
done
 
Old 03-19-2010, 12:58 PM   #4
neo2k
LQ Newbie
 
Registered: Aug 2008
Posts: 21

Original Poster
Rep: Reputation: 15
Thanks a lot colucix..getline command was very helpful, that was the key thing i was missing.

Thanks mario for the alternate solution
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
awk gsub() command - string (column) manipulation - substitution casperdaghost Linux - Newbie 1 03-08-2010 02:12 AM
Bash file manipulation help netmaster Programming 2 03-08-2008 05:01 AM
String manipulation in bash dalmat Linux - Software 4 12-03-2007 05:50 PM
string manipulation in BASH ovince Programming 4 04-16-2007 07:15 PM
bash + string manipulation dave bean Programming 7 02-16-2005 11:16 AM


All times are GMT -5. The time now is 06:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration