Operating System - HP-UX
1847161 Members
6409 Online
110263 Solutions
New Discussion

Re: awk script for changing a constant string in all rows with a variable string

 
SOLVED
Go to solution
Inter_1
Frequent Advisor

awk script for changing a constant string in all rows with a variable string

Hi,

I have one file "test" and another file "range". See below the data. I want to create another file as below called RESULT that the constant data 0000 in first column to be replaced with 0001 for the first row, 0002 for the second row and so on.

test
------------------
'0000', '490685'
'0000', '490686'
'0000', '490687'
.
.
.

range
-------------------
0001
0002
0003
.
.
.
8621

RESULT
---------------------
'0001', '490685'
'0002', '490686'
'0003', '490687'
.
.
.


Thanks,
Andy
I like to fix things.
6 REPLIES 6
James R. Ferguson
Acclaimed Contributor

Re: awk script for changing a constant string in all rows with a variable string

Hi Andy:

# paste -d " " range test |awk '{print $1,$3}' > RESULT

...by the way, file's name simply 'test' are a not-so-good idea.

Regards!

...JRF...
Inter_1
Frequent Advisor

Re: awk script for changing a constant string in all rows with a variable string

Thank you for your quick response.

The result is not the expected:

after your script
------------------
0001 '490685'
0002 '490686'
0003 '490687'

requested
----------------
'0001', '490685'
'0002', '490686'
'0003', '490687'


Thanks,
Andy
I like to fix things.
Heiner E. Lennackers
Respected Contributor

Re: awk script for changing a constant string in all rows with a variable string

paste -d " " range test | awk -v X="'" '{print X $1 X ", " $3}' > RESULT
if this makes any sense to you, you have a BIG problem
James R. Ferguson
Acclaimed Contributor
Solution

Re: awk script for changing a constant string in all rows with a variable string

Hi (again) Andy:

OK, I see, you want the first column encapsulated in single quotes.

# paste -d " " ranger tester|awk '{print "\""$1"\""","$3}'|sed -e s/\"/\'/g

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: awk script for changing a constant string in all rows with a variable string

Hi (again) Andy:

Actually, for your requirement, I find Perl more straightforward:

# paste -d " " ranger tester|perl -nale 'print join ",","\047$F[0]\047",$F[2]'

...where \047 is the octal representation of a single quote.

Regards!

...JRF...
Inter_1
Frequent Advisor

Re: awk script for changing a constant string in all rows with a variable string

Thank you guys
I like to fix things.