How to update a single row in a file with sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to update a single row in a file with sed
# 1  
Old 08-30-2006
How to update a single row in a file with sed

Hi. I want to be able to update only one line in a file that matches a key using sed, but I can't seem to get this (the key is right at the beginning of each row)

Here is what I have tried so far, but every row is being updated. I want the end result to be that the input file only has the single matching record updated, and nothing else:

grep ^$1 input.txt | sed s/$OLD/$NEW/1 < input.txt > input.txt

BTW - I do not want this:

grep ^$1 input.txt | sed s/$OLD/$NEW/1

I want the rest of the unmodified records in the result also. That's why I am trying to pipe the entire input file to sed, and I was hoping that grep would locate the record to modify.

Thanks.

Brendan
# 2  
Old 08-30-2006
Can you provide and example of the input file and the resulted desire file?
# 3  
Old 08-30-2006
Sure.

Input file:

01:15
02:30
03:19
04:50
05:65
06:05

Output file:

01:15
02:30
03:27
04:50
05:65
06:05


How would I write a sed command to update 03:19 to 03:27 without
modifying the other records? Key = first two bytes of each row.

Thanks!
# 4  
Old 08-30-2006
Code:
sed '/^03/s/19/27/' input.txt


Last edited by System Shock; 08-30-2006 at 06:52 PM..
# 5  
Old 08-30-2006
T H A N K Y O U !!

That works great, thanks so much!!

If I may ask - I know the ^ means the beginning of a row, but what exactly does the /^03 mean to the sed command when it preceeds the s/ ?

Thanks again!!

Brendan
# 6  
Old 08-30-2006
The command reads:
"for lines that start with the characters 03, substitute the first instance of 19 with 27"
# 7  
Old 08-30-2006
Nice... THANK YOU! You really helped me out.

All the best.

Brendan
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Splitting single row into multiple rows based on for every 10 digits of last field of the row

Hi ALL, We have requirement in a file, i have multiple rows. Example below: Input file rows 01,1,102319,0,0,70,26,U,1,331,000000113200000011920000001212 01,1,102319,0,1,80,20,U,1,241,00000059420000006021 I need my output file should be as mentioned below. Last field should split for... (4 Replies)
Discussion started by: kotra
4 Replies

2. Shell Programming and Scripting

Create multiple files from single file based on row separator

Hello , Can anyone please help me to solve the below - Input.txt source table abc col1 char col2 number source table bcd col1 date col2 char output should be 2 files based on the row separator "source table" abc.txt col1 char (6 Replies)
Discussion started by: Pratik4891
6 Replies

3. UNIX for Dummies Questions & Answers

Sftp transfer doing as single row

Hi All, I have a file in windows when I transfer it using sftp it is getting as single line. I tried to do both ascii and binary File: is transfered as Can you please let me know how can do the transfer as same as XML file Thanks, Arun (1 Reply)
Discussion started by: arunkumar_mca
1 Replies

4. Shell Programming and Scripting

Search row by row from one file to another file if match is found print few colums of file 2

this is the requirement list.txt table1 table2 table3 testfile.txt name#place#data#select * from table1 name2#place2#data2#select * from table 10 innerjoin table3 name2#place2#data2#select * from table 10 output name place table1 name2 place table3 i tried using awk (7 Replies)
Discussion started by: vamsekumar
7 Replies

5. Shell Programming and Scripting

Subtracting each row from the first row in a single column file using awk

Hi Friends, I have a single column data like below. 1 2 3 4 5 I need the output like below. 0 1 2 3 4 where each row (including first row) subtracting from first row and the result should print below like the way shown in output file. Thanks Sid (11 Replies)
Discussion started by: ks_reddy
11 Replies

6. Shell Programming and Scripting

Combining multiple rows in single row based on certain condition using awk or sed

Hi, I'm using AIX(ksh shell). > cat temp.txt "a","b",0 "c",bc",0 "a1","b1",0 "cc","cb",1 "cc","b2",1 "bb","bc",2 I want the output as: "a","b","c","bc","a1","b1" "cc","cb","cc","b2" "bb","bc" I want to combine multiple lines into single line where third column is same. Is... (1 Reply)
Discussion started by: samuelray
1 Replies

7. Shell Programming and Scripting

Convert columns to single row

Hello all I have data like 1 2 3 4 5 I wish my output would be like 1,2,3,4,5 For this i have executed 'BEGIN {FS="\n"; ORS=","} {print $0}' test and got the output as 1,2,3,4,5, I do not want to have , at the end of 5. output should be like (5 Replies)
Discussion started by: vasuarjula
5 Replies

8. UNIX for Advanced & Expert Users

Converting rows to a single row

Hi all I have a file as below : Development System User Production i want to convert the file to below format: "Development","System","User","Production" Is it possible with UNIX ? if so can you please give me some direction on it ? Thanks, Satya Use code tags please, ty. (10 Replies)
Discussion started by: satyaranjon
10 Replies

9. Shell Programming and Scripting

deleting blank line and row containing certain words in single sed command

Hi Is it possible to do the following in a single command /usr/xpg4/bin/sed -e '/rows selected/d' /aemu/CALLAUTO/callauto.txt > /aemu/CALLAUTO/callautonew.txt /usr/xpg4/bin/sed -e '/^$/d' /aemu/CALLAUTO/callautonew.txt > /aemu/CALLAUTO/callauto_new.txt exit (1 Reply)
Discussion started by: aemunathan
1 Replies

10. Shell Programming and Scripting

to append data in a single row

Hi all, I need a logic in UNIX to append contents of a file in same row, where the original contents are found one below the other. For example. My i/p file contains: “user1”,”employee1”,”04/28/2009” “5678” “78” “user2”,”employee2”,”04/30/2009” I want my output... (8 Replies)
Discussion started by: Sgiri1
8 Replies
Login or Register to Ask a Question