Computing.Net > Forums > Linux > how to replace a record

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.

how to replace a record

Reply to Message Icon

Name: yacob
Date: October 7, 2008 at 05:45:29 Pacific
OS: Linux
CPU/Ram: 2GB
Product: linux
Comment:

Hi,

I have a script as below and I would like to replace the first field of record 5th of the file parameter.PAR by the file name that I read as line.

#!/bin/bash
for line in *.mod;

do

awk '{ if (NR == 5) { $1=$line } }' parameter.PAR > out

done

But this seems does not work. Any comments please

yacob



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: October 7, 2008 at 07:07:27 Pacific
Reply:

There are several ways of using shell parameters in awk scripts. Check out this link:

http://www.tek-tips.com/faqs.cfm?fi...


0

Response Number 2
Name: yacob
Date: October 7, 2008 at 08:21:01 Pacific
Reply:


when I used this modified script

for i in *.mod


do
awk '{ if (NR == 5) { $1=$i ; print } else print $0}' out1 > out

done

the "out" output file is not modified by the file name "$i". I got the same as the original fie only the second column of the 5th recored repeated.

Any help, please
Yacob


0

Response Number 3
Name: yacob
Date: October 7, 2008 at 09:37:51 Pacific
Reply:


by defining a variable
as:

arg=$i

awk -v var=$arg '{ if (NR == 5) { $1=var ; print } else print $0}' out1 > out

it seems that I have solved the problems. But if you have more elegant ways please comment.


0

Response Number 4
Name: nails
Date: October 7, 2008 at 11:12:35 Pacific
Reply:

Three things:

1) I see no reason for arg=$i. var="$i" will work as well.

2) Note the double quotes around "$i" above. If your argument contains white space, it won't pass properly to the awk script without the quotes.

3) Under Linux, your use of the -v option is correct. Most Linux distros use gawk which is POSIX compliant. If you tried to run that on older versions of AIX and solaris it would fail.

The method below is more portable:

awk '{ if (NR == 5) { $1=var ; print } else print $0}'var="$i" out1


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


which one?? porting a module to a lin...



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: how to replace a record

how to create a dual boot www.computing.net/answers/linux/how-to-create-a-dual-boot/1301.html

How to install a Zip Drive, Floppy Disk, CD-ROM and a CD-RW www.computing.net/answers/linux/how-to-install-a-zip-drive-floppy-disk-cdrom-and-a-cdrw/90.html

how to create a Boot Diskette www.computing.net/answers/linux/how-to-create-a-boot-diskette/9865.html