oUo
April 21, 2012, 10:38am
1
Dear board,
(I am trying to post this the 3rd time, seems there's some conflicts with my firefox with this forum, now use IE)
------
yes, I have searched the forum, but seems my ? is too complicated.
------------origianl file ---------------
\storage\qweq\ertert\ertert\3452\&234\test.rec
qweqweqe qweq234erg567&(*&234234
...
\storage\qweq\xvx\er6578t\3452\&234\test.rec
...
\storage\3545\ertert\ertert\3452\&234\test.rec
...
----------what I want-------------------
\newplace\10000.rec
qweqweqe qweq234erg567&(*&234234
...
\newplace\10001.rec
....
\newplace\10002.rec
...
\newplace\10003.rec
===================
I have tried a shell script to get first the link list and then use a "while do"
but not successful.
Really appreciate any advice
Thanks in advance!
agama
April 21, 2012, 2:19pm
2
Based on your sample data, it seems that for every record that ends with "test.rec" you wish to make the change and all other lines are to be left as is. If that is correct, then this should work:
awk ' BEGIN { c=10000; }
/test.rec$/ { printf( "\\newplace\\%d.rec\n", c++ ); next; }
{ print; } ' input-file >output-file
Shell:
i=10000
while IFS= read -r line; do
case $line in
\\storage*) printf "%s%05d\n" '\newplace\' $((i+=1)) ;;
*) printf "%s\n" "$line" ;;
esac
done < infile
oUo
April 23, 2012, 10:21am
4
Thanks agama & Scrunitzer!
I am trying the method of agama, it is one line... But How if the newplace is also a variable, i.e. NewPath=newplace
but withe awk line, both symbols of ' & " have already used, how to transfer the variable into that line?
i.e.
awk ' BEGIN { c=10000; } /test.rec$/ { printf( "$NewPath\\%d.rec\n", c++ ); next; } { print; } ' input-file >output-file
But $NewPath cannot be recognized.
agama
April 23, 2012, 6:12pm
5
I assume you have defined NewPlace as a shell variable. If so, you can import it into the awk in one of two ways:
awk ' BEGIN { c=10000; }
/test.rec$/ { printf( "%s\\%d.rec\n", newpath, c++ ); next; }
{ print; } ' newpath="$NewPath" input-file >output-file
awk -v newpath="$NewPath" ' BEGIN { c=10000; }
/test.rec$/ { printf( "%s\\%d.rec\n", newpath, c++ ); next; }
{ print; } ' input-file >output-file
In both cases the awk variable newpath
is assigned at the start of the script with the value of the shell variable. Some versions (older) of awk don't support the -v option, so I've given you both formats.