Operating System - HP-UX
1834772 Members
2943 Online
110070 Solutions
New Discussion

Re: Please help me, need help to modify a file using awk or sed

 
SOLVED
Go to solution
Vivek_3
Advisor

Please help me, need help to modify a file using awk or sed

Hi to all,

I need to relese a script by today itself. please help me to do this.

I have a big file lets say 65 thousand lines. it is a tab seperated text file. i need to add one more columne at the end of every line ( tab seperated). the value of that column is the first column of the first line.

example:

Inputfile:

NY^hhhhh^hhhhh^kkkkk^gggg
hhh^jjjjj^9999^44444
888^hhhh^443444^hhhhhh

Output file

NY^hhhhh^hhhhh^kkkkk^gggg^NY
hhh^jjjjj^9999^44444^NY
888^hhhh^443444^hhhhhh^NY

here '^' represent tab character.


please help me ASAP.

vivek S.
viveks@fox.com
15 REPLIES 15
Sanjay_6
Honored Contributor

Re: Please help me, need help to modify a file using awk or sed

Hi Vivek,

Use,

sed "s/$/^NY/g" OLD_FILE >NEW_FILE

Hope this helps.

Regds
James R. Ferguson
Acclaimed Contributor

Re: Please help me, need help to modify a file using awk or sed

Hi:

Consider this:

# echo "a 123\nb 456\nc 789"|awk '{if (NR==1) X=$1;print $0"\t"X}'

Thus,

# awk '{if (NR==1) X=$1;print $0"\t"X}' myfile.in > myfile.out

Regards!

...JRF...
Craig Rants
Honored Contributor

Re: Please help me, need help to modify a file using awk or sed

Create an awk script file called awkfile and put the following code in it.

BEGIN { FS ="\t" }

/^[a-z]/ {print $0, $1}
/^[A-Z]/ {print $0, $1}
/^[0-9]/ {print $0, $1}

Then call the script by

awk -f awkfile yourfilename > yourfilename1

GL,
C
"In theory, there is no difference between theory and practice. But, in practice, there is. " Jan L.A. van de Snepscheut
Craig Rants
Honored Contributor

Re: Please help me, need help to modify a file using awk or sed

oops, did read all the way through.
I'll modify to fit.

C
"In theory, there is no difference between theory and practice. But, in practice, there is. " Jan L.A. van de Snepscheut
Sridhar Bhaskarla
Honored Contributor
Solution

Re: Please help me, need help to modify a file using awk or sed

Hi Vivek,

1. Get the first word in the fist line (Do you have comments?)

WORD=`head -1 file |awk '{print $1}'`

2. Replace the end of line with $WORD

sed 's/$/ '$WORD'/' file > result

Note the space after $/

-Sri

You may be disappointed if you fail, but you are doomed if you don't try
Darrell Allen
Honored Contributor

Re: Please help me, need help to modify a file using awk or sed

Hi Vivek,

In case you don't have what you want yet...

Using awk (one liner):
awk -v EXT=`head -1 infile | awk '{print $1}'` '{print $0"\t"EXT}' infile

Using sed:
EXT=`head -1 infile | awk '{print $1}'`
sed "s/$/ $EXT/" infile

Note the "white space" before $EXT in the sed example is a single tab character.

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Vivek_3
Advisor

Re: Please help me, need help to modify a file using awk or sed

Thanks to all of you for putting your time and effort here to help me out.

since i am using Sridhar's idea, so 10 points goes to Sridhar.

thanks again.

vivek
Sridhar Bhaskarla
Honored Contributor

Re: Please help me, need help to modify a file using awk or sed

Hi Vivek,

I am feeling guilty now.

You need to give points to everyone or ignore all. It is not that we sell our ideas here for points. And moreover it is not about points but the feedback and saying thanks to others for spending their time for you. You will not run out of points if you keep giving them.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
James R. Ferguson
Acclaimed Contributor

Re: Please help me, need help to modify a file using awk or sed

Hi:

For the record, you can assign points to no one, some, or all, equally, or on a scale based on the merit, *regardless* of which solution you ultimately choose. See here:

http://us-support2.external.hp.com/estaff/bin/doc.pl/forward/screen=estaffAssistance/sid=748a3f351af90e834c?Page=file0002#forpoints

Regards!

...JRF...
Vivek_3
Advisor

Re: Please help me, need help to modify a file using awk or sed

Sorry for this time but it won't happen again. i am going to assign points to evryone here.

thanks
Vivek_3
Advisor

Re: Please help me, need help to modify a file using awk or sed

Hi,

i was trying following command and it was not working. i don't know why? please someone explain me.

vv=`head -1 BP|awk '{print substr($0,1,2)}'`
echo $vv
awk '{print $0,$vv}' BP >BP_3

please help me to understand awk command.

thanks.
James R. Ferguson
Acclaimed Contributor

Re: Please help me, need help to modify a file using awk or sed

Hi Vivek:

OK, you're taking the first line of a file named "BP" and assigning the first two characters of the first line to the variable 'vv'.

Then, you echo the value of 'vv' to stdout.

Lastly, you are attempting to append the value of 'vv' to the end of each line of the file 'BP' creating file 'BP_3'.

BUT, your syntax is wrong, It should be:

# awk -v vv=$vv '{print $0,vv}' BP > BP_3

...OR...

# awk '{print $0,vv}' vv=$vv BP > BP_3

Regards!

...JRF...
Vivek_3
Advisor

Re: Please help me, need help to modify a file using awk or sed

Thanks James. Now i know what was wrong.
thanks
James R. Ferguson
Acclaimed Contributor

Re: Please help me, need help to modify a file using awk or sed

Hi (again):

Excuse me. If I helped you, please remember to assign points for my time and effort. Thank you.

...JRF...
Vivek_3
Advisor

Re: Please help me, need help to modify a file using awk or sed

I would love to assign points but i really don't see a way to do so.

Thanks