Replace first field with Rownum in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace first field with Rownum in a file
# 1  
Old 11-23-2010
Replace first field with Rownum in a file

Hi,

I've a file which is "|" delimited. I want to replace the first field with rownum, how can I do it with awk or sed command? There is no header record.

sample file
Code:
1195|ABC|DEF|30 LATE DR||CHARLOTTE|NC||NONE@NONE.COM|I|1
1227|PQR|STU|3 KING DR||LIVONIA|NJ|481524071|YAHOO@YAHOO.COM|I|3
1428|RST|UVW|2 BORNE RD||SAINT|GA|315589109|GMAIL@GMAIL.COM|H|2

Results
Code:
1|ABC|DEF|30 LATE DR||CHARLOTTE|NC||NONE@NONE.COM|I|1
2|PQR|STU|3 KING DR||LIVONIA|NJ|481524071|YAHOO@YAHOO.COM|I|3
3|RST|UVW|2 BORNE RD||SAINT|GA|315589109|GMAIL@GMAIL.COM|H|2


Last edited by Franklin52; 11-24-2010 at 05:25 AM.. Reason: Code tags
# 2  
Old 11-23-2010
Code:
awk -F'|'  '{$1=FNR; print $0}'  infile > outfile

use nawk on Solaris
# 3  
Old 11-23-2010
Code:
awk -F\| '$1=NR' OFS=\| file

This User Gave Thanks to cabrao For This Post:
# 4  
Old 11-23-2010
Code:
awk -F\| 'NF==11{$1=++i}1' OFS=\|  infile

# 5  
Old 11-24-2010
Code:
perl -ne 's/^\d+/$./;print;'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace field in the delimited file

Hi, I have the requirement similar to the one mentioned in the below thread. https://www.unix.com/unix-for-dummies-questions-and-answers/128155-search-replace-string-only-particular-column-delimited-file.html The only difference is that I need to change the field for row 1,2 and the last... (14 Replies)
Discussion started by: chetanojha
14 Replies

2. Shell Programming and Scripting

Need to replace last field in a file,if first field matches

Hi, Need to replace last field in a file(/etc/passwd) ,if first filed matches with particular username. Scenario: cat testfor1 deekshi:x:7082:7082::/home/deekshi:/bin/bash harini1:x:7083:7083::/home/harini1:/bin/bash Here,if first field contains "deekshi", then i should replace... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

3. Shell Programming and Scripting

Replace pattern from nth field from a file

I have posted this again as old post is closed and I am not able to reopen. so please consider this new post Input File : 1,A,Completed,06.02_19.36,Jun 30 20:00 2,BBB,Failed,07.04_05.12,Jul 21 19:06 3,CCCCC,New,07.21_03.03,Jul 26 12:57 4,DDDDD,Pending,, I wast output file as: ... (7 Replies)
Discussion started by: Amit Joshi
7 Replies

4. Shell Programming and Scripting

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... (9 Replies)
Discussion started by: Melah Gindi
9 Replies

5. Shell Programming and Scripting

Replace field in one file with whole record data of another

Hello Group, I need to replace the city field in “File 1 (fileld 3), with the entire record line of “File 2” (including delimiters) where the “city” field (File 1, Field 3)matches city field (File 2, Field1). All of the other data in “File 1” should remain intact(Fields 1,2,4,5,6). Only field... (1 Reply)
Discussion started by: vestport
1 Replies

6. Shell Programming and Scripting

Replace a particular field in all records in a csv file

hi, i have various csv files, the file format is as follows Entry: "1",4,2010/08/15-10-00-00.01,,"E",,,,,,,,,120,0,"M4_","C","KEW-011-5337140-20100916163456-540097","1234567890","N N 0 ",,,"NUK 800100200",,,"NN",,,,,,,,,,,,"0000000001|0001|20150401... (2 Replies)
Discussion started by: niteesh_!7
2 Replies

7. Shell Programming and Scripting

Replace third field of the first record in a file....

Hi, I am new to unix and am trying to do something below: I have a pipe delimited file with millions of records. I need to replace the third column of the first record to the number of lines in the file. How can I do that. Will appreciate any advice and help. Thanks Simi (3 Replies)
Discussion started by: simi28
3 Replies

8. Shell Programming and Scripting

Help to replace a field in one file with a field from another file

I have to replace a field in one file with a field from other file. I came across this awk command to replace a field with one string nawk -F'|' -v OFS='|' '$2="replace"' temp2 > temp3 I need to have something like cut -f2 -d "|" temp1 (a field from other file) instead of 'replace' Is... (8 Replies)
Discussion started by: savant
8 Replies

9. Shell Programming and Scripting

replace a field in a CSV file

Hello all, I've a CSV file and need to replace 5th field if its value is "X". The exact requirement is to replace 5th field (column) with "Y" if a. it's value is "X" AND b. the line must start with ABC string i guess this can be done with awk. Pl help. For security reasons, the... (2 Replies)
Discussion started by: prvnrk
2 Replies

10. Shell Programming and Scripting

replace a similar field in a file

Hello, I am having a file where I have to replace the port values with the variable I defined. The file is an extract of an xml file: <NameValuePair> <name>Service1</name> <value>tcp:32406</value> </NameValuePair> <NameValuePair> ... (2 Replies)
Discussion started by: chiru_h
2 Replies
Login or Register to Ask a Question