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
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
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.
Summary: Hope this page help solve your problem: Red Hat Linux 6.2: The Official Red Hat Linux Reference Guide Prev Next -------------------------------------------------------------------------------- Appe...
Summary: I know how to install a CD-ROM and a CD-RW, but I have only 3 IDE ports and 1 for the floppy, and I need to connect 4 things plus the hard drive, so I need to make one of the CD-Roms a slave of the ot...