Replace a field with line number in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace a field with line number in file
# 1  
Old 10-31-2013
Tools Replace a field with line number in file

I am working on a script to convert bank data to a csv file. I have the format done - columns etc. The final piece of the puzzle is to change the second field (after the R) of every line to reflect its' line number in the file. I am stumped. I can use awk on each line but need help looping through the file.

Sample file below:

Code:
 
"R","","t0000123","","10/29/2013","10/29/2013","","","","100.00"

Any assistance greatly appreciated!
# 2  
Old 10-31-2013
try:
Code:
awk '$2="\"" NR "\""' FS=, OFS=, infile

This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 10-31-2013
in perl:
Code:
 perl -pe 's/""/$./' input

This will replace first "" one with current line number as per your input sample.
This User Gave Thanks to greet_sed For This Post:
# 4  
Old 10-31-2013
Sed version

Code:
$ cat <<eof | sed -e 's/""/1/'
"R","","t0000123","","10/29/2013","10/29/2013","","","","100.00"
eof

"R",1,"t0000123","","10/29/2013","10/29/2013","","","","100.00"

OR

Code:
$ cat <<eof | awk '!x{x=sub(/""/,1)}1'
"R","","t0000123","","10/29/2013","10/29/2013","","","","100.00"
eof

"R",1,"t0000123","","10/29/2013","10/29/2013","","","","100.00"


Last edited by Akshay Hegde; 10-31-2013 at 04:00 PM.. Reason: To give awk sol
# 5  
Old 10-31-2013
@ akshay:
your solution will give always 1 as a replacement.

in sed we can try with
Code:
=

but i didnt succeed to replace in the required field.
# 6  
Old 10-31-2013
@greet_sed try this awk

Code:
$ cat <<eof | awk 'x="";!x{x=sub(/""/,NR)}1' 
 "R","","t0000123","","10/29/2013","10/29/2013","","","","100.00"
"R","","t0000123","","10/29/2013","10/29/2013","","","","100.00"
eof

 "R",1,"t0000123","","10/29/2013","10/29/2013","","","","100.00"
"R",2,"t0000123","","10/29/2013","10/29/2013","","","","100.00"

# 7  
Old 10-31-2013
Quote:
Originally Posted by greet_sed
in sed we can try with
Code:
=

but i didnt succeed to replace in the required field.
That cannot work because = does not modify the pattern space. = writes directly to stdout and there is no way to capture its output to strip newlines from within the sed instance itself.

Regards,
Alister
This User Gave Thanks to alister For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace a number in the last line of a delimited file.

Hi all, I am fairly new to UNIX and I was wondering if you could provide me with some help! Lets say i have a file as below : Line 1 Line 2 Line 3 ABC|12|4|2 Now the number 4 in bold, this number will represent the number of row there is in the file excluding the header and footer... (10 Replies)
Discussion started by: Stinza
10 Replies

2. Shell Programming and Scripting

awk to find number in a field then print the line and the number

Hi I want to use awk to match where field 3 contains a number within string - then print the line and just the number as a new field. The source file is pipe delimited and looks something like 1|net|ABC Letr1|1530||| 1|net|EXP_1040 ABC|1121||| 1|net|EXP_TG1224|1122||| 1|net|R_North|1123|||... (5 Replies)
Discussion started by: Mudshark
5 Replies

3. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

4. Shell Programming and Scripting

Replace first field of a line with previous filed of the line

Hi Everyone, I have a file as below: IM2345638,sherfvf,usha,30 IM384940374,deiufbd,usha,30 IM323763822,cdejdkdnbds,theju,15 0,dhejdncbfd,us,20 IM398202038,dhekjdkdld,tj,30 0,foifsjd,u2,40 The output i need is as below IM2345638,sherfvf,usha,30... (4 Replies)
Discussion started by: usha rao
4 Replies

5. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

6. Shell Programming and Scripting

Replace first number of each line in a file with another number

Hi, I have a set of files in a directory that I have to read and replace the first occurrence of a number with another dummy number. This is what I have so far but it does not seem to work. The files have lot of other data in each row and each data element is separated by ,@, for file in... (13 Replies)
Discussion started by: scorpioraghu
13 Replies

7. Shell Programming and Scripting

sed to replace a field from a line with another field

i have something like this, cat filename.txt hui this si s"dfgdfg" omeone ipaddress="10.19.123.104" wel hope this works i want to replace only 10.19.123.104 with different ip say 10.19.123.103 i tried this sed -i "s/'ipaddress'/'ipaddress=10.19.123.103'/g" filename.txt ... (1 Reply)
Discussion started by: vivek d r
1 Replies

8. Shell Programming and Scripting

Replace 2nd column for each line in a csv file with fixed string+random number

Hi experts, My csv file looks like this U;cake;michael;temp;;;; U;bread;john;temp;;;; U;cocktails;sarah;temp;;;; I'd like to change the value fo 2nd column to cf+random number , which will look maybe something like this U;cf20187;michael;temp;;;; U;cf8926;john;temp;;;;... (7 Replies)
Discussion started by: tententen
7 Replies

9. Shell Programming and Scripting

To trim Certain field in a line of a file and replace the new string in that position

To trim 3rd field in for all the lines of a file and replace the modified string in that particular field. For example i have a file called Temp.txt having content Temp.txt ----------------- 100,234,M1234 400,234,K1734 300,345,T3456 ---------------- So the modified file output should... (4 Replies)
Discussion started by: rpadhi
4 Replies

10. Shell Programming and Scripting

fourth field is number in a line

hi i hav a file like 121212 asdd d 7 dfsdffdffsdfsdfsdfdf rrretrtrtre 121212 asdd d 5 dfsdffdffsdfsdfsdfdf rrretrtrtre 121212 asdd d 5 dfsdffdffsdfsdfsdfdf rrretrtrtre 121212 asdd d 4 dfsdffdffsdfsdfsdfdf rrretrtrtre 121212 asdd d 6 dfsdffdffsdfsdfsdfdf rrretrtrtre i need to... (4 Replies)
Discussion started by: Satyak
4 Replies
Login or Register to Ask a Question