sed&awk: replace lines with counting numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed&awk: replace lines with counting numbers
# 1  
Old 04-21-2012
sed&awk: replace lines with counting numbers

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 ---------------
Code:
\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-------------------
Code:
\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!

Moderator's Comments:
Mod Comment Welcome to the UNIX and Linux Forums. Please use code tags. Video tutorial on how to use them

Last edited by Scrutinizer; 04-21-2012 at 03:57 PM..
# 2  
Old 04-21-2012
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:

Code:
awk ' BEGIN { c=10000; }
    /test.rec$/ { printf( "\\newplace\\%d.rec\n", c++ ); next; }
    { print; } ' input-file >output-file

# 3  
Old 04-21-2012
Shell:
Code:
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


Last edited by Scrutinizer; 04-21-2012 at 04:40 PM..
# 4  
Old 04-23-2012
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.
Code:
awk ' BEGIN { c=10000; } /test.rec$/ { printf( "$NewPath\\%d.rec\n", c++ ); next; }  { print; } ' input-file >output-file

But $NewPath cannot be recognized.
# 5  
Old 04-23-2012
I assume you have defined NewPlace as a shell variable. If so, you can import it into the awk in one of two ways:

Code:
awk ' BEGIN { c=10000; } 
   /test.rec$/ { printf( "%s\\%d.rec\n", newpath, c++ ); next; }  
   { print; } '  newpath="$NewPath" input-file >output-file



Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed to replace / between the two numbers

i Have a file as following view pz19a0c0/1000T_J_3MoDw9DSLh1ZsCubdua-LKOQmbtiVgkIsiMbSiwF467?sessionId=15451401994597121249 view pz19a0c0/100086X67pR0MwzWnhhSO6sAEoxeFMyhh-IIbUCCdxicaQM4FC9?sessionId=154514019945971212494898 view/cart ... (5 Replies)
Discussion started by: Raghuram717
5 Replies

2. UNIX for Dummies Questions & Answers

Sed/awk to find negative numbers and replace with 1?

Greetings. I have a three column file, and there are some numbers in the second column that are <1. However I need all numbers to be positive, thus need to replace all those numbers with just one. I feel like there must be a simple way to use awk to find these numbers and sed to replace but can't... (5 Replies)
Discussion started by: Twinklefingers
5 Replies

3. Shell Programming and Scripting

Counting lines in a file using awk

I want to count lines of a file using AWK (only) and not in the END part like this awk 'END{print FNR}' because I want to use it. Does anyone know of a way? Thanks a lot. (7 Replies)
Discussion started by: guitarist684
7 Replies

4. UNIX for Dummies Questions & Answers

Replace lines of two files by the corresponding line numbers.

I want to replace lines. The files 1 are (separated by \t) Gm01 phytozome9_0 three_prime_UTR 70641 70759 . - . ID=PAC:26323927.three_prime_UTR.1;Parent=PAC:26323927;pacid=26323927 Gm01 phytozome9_0 three_prime_UTR 90230 90692 . - . ... (1 Reply)
Discussion started by: grace_shen
1 Replies

5. Shell Programming and Scripting

Sed/awk/perl command to replace pattern in multiple lines

Hi I know sed and awk has options to give range of line numbers, but I need to replace pattern in specific lines Something like sed -e '1s,14s,26s/pattern/new pattern/' file name Can somebody help me in this.... I am fine with see/awk/perl Thank you in advance (9 Replies)
Discussion started by: dani777
9 Replies

6. Shell Programming and Scripting

Running sed and counting number of lines processed

/bin/sed -n ';4757335,$ p' | wc -l /bin/sed -n ';4757335,$ p' | egrep "Failed" | egrep -c "PM late arrrival" how can i combine the above two sed commands into one? i want to count the number of lines between the specified line number and the end of the file. AND and i want to count how many... (5 Replies)
Discussion started by: SkySmart
5 Replies

7. UNIX for Dummies Questions & Answers

Selective Replacements: Using sed or awk to replace letters with numbers in a very specific way

Hello all. I am a beginner UNIX user who is using UNIX to work on a bioinformatics project for my university. I have a bit of a complicated issue in trying to use sed (or awk) to "find and replace" bases (letters) in a genetics data spreadsheet (converted to a text file, can be either... (3 Replies)
Discussion started by: Mince
3 Replies

8. Shell Programming and Scripting

Help with awk array syntax & counting script

..... (3 Replies)
Discussion started by: elbee11
3 Replies

9. Shell Programming and Scripting

awk/sed to search & replace data in first column

Hi All, I need help in manipulating the data in first column in a file. The sample data looks like below, Mon Jul 18 00:32:52 EDT 2011,NULL,UAT Jul 19 2011,NULL,UAT 1] All field in the file are separated by "," 2] File is having weekly data extracted from database 3] For eg.... (8 Replies)
Discussion started by: gr8_usk
8 Replies

10. Shell Programming and Scripting

sed counting lines

Hi, I'm using the command: sed -n '$=' $1 on a sh script on AIX. This script is used to count the number of lines of files. If the file has no lines at all the command doesn't return nothing. I need the command to return 0 when the file has no lines at all. How can I achieve this? Best... (5 Replies)
Discussion started by: jppedroso
5 Replies
Login or Register to Ask a Question