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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace first field of a line with previous filed of the line
# 1  
Old 08-06-2013
Replace first field of a line with previous filed of the line

Hi Everyone,

I have a file as below:

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


Code:
 
IM2345638,sherfvf,usha,30
IM384940374,deiufbd,usha,30
IM323763822,cdejdkdnbds,theju,15
IM323763822,dhejdncbfd,us,20
IM398202038,dhekjdkdld,tj,30
IM398202038,foifsjd,u2,40

Here I want to replace 0 with first field of previous line.

Not sure whether to use awk or sed

I tried this way but it didnt work as it is just printing first field.

Code:
for i in `cat test.txt | awk -F, '{print $1}'`; do if [ $i == 0 ]; then i=$var; fi; echo $i ; var=$i; done

Thanks in advance
Usha

Last edited by usha rao; 08-06-2013 at 07:46 AM.. Reason: code tags
# 2  
Old 08-06-2013
Code:
awk -F',' 'NR==1 {p=$1} {if($1 == 0) {$1=p} else {p=$1}}1' file

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 08-06-2013
Try:

Code:
awk -F',' 'BEGIN { OFS=","; p=0 } $1 != 0 { p = $1 } { $1 = p }1' file

This User Gave Thanks to agn For This Post:
# 4  
Old 08-06-2013
Another approach:
Code:
awk -F, '!$1{$1=s}{s=$1}1' OFS=, file

This User Gave Thanks to Franklin52 For This Post:
# 5  
Old 08-06-2013
Thanks a lot Balajesuri and Agn.
This is exactly what I need and made my work simpler.
Thanks you very much, I really appreciate this.

---------- Post updated at 04:34 AM ---------- Previous update was at 04:34 AM ----------

Thank You Franklin52 Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

How to find a string in a line in UNIX file and delete that line and previous 3 lines ?

Hi , i have a file with data as below.This is same file. But actual file contains to many rows. i want to search for a string "Field 039 00" and delete that line and previous 3 lines in that file.. Can some body suggested me how can i do using either sed or awk command ? Field 004... (7 Replies)
Discussion started by: vadlamudy
7 Replies

2. 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

3. Shell Programming and Scripting

Search a string,get line and replace with second field

Hi, I need to search for source path in file2 , as per file1 and if found get the next line and take the field value and put it in URL value of file1. In file1, NF is not same for all the lines. file1: <type source="/home/USER/Desktop" Dest="/home/USER/DIR1/Desktop" URL="ssh/path"/> <type... (8 Replies)
Discussion started by: greet_sed
8 Replies

4. 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

5. Shell Programming and Scripting

Compare Field in Current Line with Field in Previous

Hi Guys I have the following file Essentially, I am trying to find the right awk/sed syntax in order to produce the following 3 distinct files from the file above: Basically, I want to print the lines of the file as long as the second field of the current line is equal to the... (9 Replies)
Discussion started by: moutaye
9 Replies

6. Shell Programming and Scripting

Perl: Conditional replace based on previous and current value in a line

I need to read the contents of a file. Then I need to grep for a keyword and replace part of the grepped line based on the condition of previous and present line. Example input file: V { port1 = P; port2 = 0; shift_port = P0; /* if next shift_port is P0 I need... (9 Replies)
Discussion started by: naveen@
9 Replies

7. Shell Programming and Scripting

Print a field from the previous line

plz help me!! I have this file , 3408 5600 3796 6035 4200 6285 4676 0 40 1554 200 1998 652 2451 864 2728 1200 0 I want it like if $2==0,replace it with field from the previous line+500 say here the o/p would be like 3408 5600 3796 6035 4200 6285... (16 Replies)
Discussion started by: Indra2011
16 Replies

8. UNIX for Dummies Questions & Answers

howto replace field in line

Hi all, I'm writing a script in bash where i'm reading line by line into variable, and the i want to replace the second filed in the line with another number, for example if the line looks like this: 10 20 30 40 50 it should look like this: 10 90 30 40 50 and put the new result inside... (1 Reply)
Discussion started by: morfix
1 Replies

9. UNIX for Dummies Questions & Answers

Find and replace a field in the last line

I have a file 'test.out' with contents: 1|1|10|10|I|asdf| 2|1|10|10|I|sdfg| 4|1|10|10|I|hgfj| 34|0|10|10|I|sdg| I want to modify the fifth column with value 'I' to 'A' for only the last line. Below is what I expect to see: 1|1|10|10|I|asdf| 2|1|10|10|I|sdfg| ... (3 Replies)
Discussion started by: ChicagoBlues
3 Replies

10. Shell Programming and Scripting

replace [previous] line

Hi ,suppose I have a file title=dsafsadf ........ ....... year=1995 author=john smith ............ title=bbbbbb ........ ....... year=1988 author=alex I need to replace the title line with a expression that contains variables year and author. I want to use a python readline for loop.... (10 Replies)
Discussion started by: grossgermany
10 Replies
Login or Register to Ask a Question