Sed find and insert


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed find and insert
# 1  
Old 04-27-2010
Sed find and insert

Hi All

I'm trying to insert a pattern if a pattern is found in a file.

This is my sample file

Code:
"PDA"|"Celvin"|"PRJ_NA"|"Completion_Units"|25
"PDA"|"Celvin"|"PRJ_AB"|"Completion_Units"|250

I would like to output as

Code:
"PDA"|"Celvin"|"PRJ_NA"|"Completion_Units"|"Done"|25
"PDA"|"Celvin"|"PRJ_AB"|"Completion_Units"|"Done"|250

I wrote a sed command in (cygwin)which is

Code:
sed ':a;s/"|[0-9]/|"Done"|/g' Samp.dat >tmpfil.txt

However the output of the command is

Code:
"PDA"|"Celvin"|"PRJ_NA"|"Completion_Units"|"Done"|5
"PDA"|"Celvin"|"PRJ_AB"|"Completion_Units"|"Done"|50

It is removing the first digit (i.e. "2" in the example)

Can some one please let me know how to solve it?

Last edited by Scott; 04-27-2010 at 07:37 PM.. Reason: Added code tags around output
# 2  
Old 04-27-2010
Hi.

Code:
$ sed 's/|[0-9]*$/|"Done"&/' file1
"PDA"|"Celvin"|"PRJ_NA"|"Completion_Units"|"Done"|25
"PDA"|"Celvin"|"PRJ_AB"|"Completion_Units"|"Done"|250

This User Gave Thanks to Scott For This Post:
# 3  
Old 04-27-2010
Thanks a ton Scott that worked Smilie

Quote:
Originally Posted by scottn
Hi.

Code:
$ sed 's/|[0-9]*$/|"Done"&/' file1
"PDA"|"Celvin"|"PRJ_NA"|"Completion_Units"|"Done"|25
"PDA"|"Celvin"|"PRJ_AB"|"Completion_Units"|"Done"|250

# 4  
Old 04-27-2010
Code:
awk -F\| '{$NF="\"Done\"|" $NF}1' OFS=\| urfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using sed to find and append or insert on SAME line

Hi, $ cat f1 My name is Bruce and my surname is I want to use SED to find “Bruce” and then append “ Lee” to the end of the line in which “Bruce” is found Then a more tricky one…. I want to INSERT ….a string… in to a line in which I find sometihng. So example $ cat f2 My name is... (9 Replies)
Discussion started by: Imre
9 Replies

2. Shell Programming and Scripting

Sed: how do I insert a \ in my replace

I'm in the process of being forward-thinking and finally converting my site's db to UTF-8. I've already done the UTF-8 conversion (on a copy for testing) and now I want to go through and convert html entities to their actual characters. I ran an entity decode on a mysqldump file but realized... (10 Replies)
Discussion started by: dheian
10 Replies

3. Shell Programming and Scripting

How do I insert text with sed ?

Hi I was wondering if anyone new of a solution to this problem? I need to copy a time stamp that is on a line of .text in a text file into multiple positions on the same line. I need to insert the time stamp on the same line between every occurance of the text ".pdf_.html" right after the... (9 Replies)
Discussion started by: Paul Walker
9 Replies

4. Shell Programming and Scripting

sed to insert a character

Hi all, I have a file like this Q8N302 21-84 Q8N157 15-45 Q99996 167-201 202-251 269-318 I want to insert a character or space if the line starts with a number and I used the command sed 's/^/#/' But in the output file, when it inserts this character, first digit in the number is... (2 Replies)
Discussion started by: kaav06
2 Replies

5. Shell Programming and Scripting

Insert text using sed

sed 's/$/TEST/g' will insert TEST at the end of each line. i want to insert TEST at column 64 (7 Replies)
Discussion started by: lawsongeek
7 Replies

6. Shell Programming and Scripting

sed - insert two lines

I have done this sed command to insert one line after a specific string is found: sed '/patternstring/ a\ new line string' file1 But how do I insert two lines? This is not possible: sed '/patternstring/ a\ new line string \a new line string 2' file1 (2 Replies)
Discussion started by: locoroco
2 Replies

7. Shell Programming and Scripting

sed to insert a separator

My txt file consists of records with 6 numbers followed by 3 characters. Is there a simple “sed” which will insert a | separator between the 6th and 7th position ? Many thanks (3 Replies)
Discussion started by: malts18
3 Replies

8. Shell Programming and Scripting

sed: how to insert tab?

Hi, I'm using the following to insert lines into file: sed ${rowNr}i'\ first row\ second row\ third row\ ' file.txt How can I add tab in front of each added line? "\t" or actual TAB does not seem to work? Thanks! (2 Replies)
Discussion started by: Juha
2 Replies

9. UNIX for Dummies Questions & Answers

Insert Text With Sed

Hello. Trying to insert text at line 1 and after last line of file. I have searched posts but nothing seems to work. I keep getting extra characters error or nothing gets inserted into the file. #!/bin/sh touch textfile.txt sed 'i\ Add this line before every line with WORD' textfile.txt ... (5 Replies)
Discussion started by: steveramsey
5 Replies

10. Shell Programming and Scripting

sed search and insert

how can i modify a file using sed when searching for a pattern and insert newline after the pattern? pattern example: $ ...(any characters)...$ $ may082001.../tmp/msg.001,,$ REPT CLEAR ALARMS ON UNIT 1 $ may082001.../tmp/msg.002,,$ UNIT 1 IN SERVICE into: $... (1 Reply)
Discussion started by: apalex
1 Replies
Login or Register to Ask a Question