Replace String at the start of each line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace String at the start of each line
# 1  
Old 12-11-2015
Replace String at the start of each line

Replace String at the start of each line

Last edited by Mahesh_RPM; 12-11-2015 at 11:27 AM..
# 2  
Old 12-11-2015
Well, What have you tried so far?

Anyways, Here it is..
Code:
sed 's/^Delete_[0-9]*_//' file_name

-Ranga
This User Gave Thanks to rangarasan For This Post:
# 3  
Old 12-11-2015
Replace String at the start of each line

Thanks, It works fine Smilie

I made a mess with the Script.
# 4  
Old 12-11-2015
I thought you said you also wanted to changed Off (or in your sample input file off) to On. Maybe you need something more like:
Code:
sed 's/^Delete_[0-9]*_//;s/|[Oo]ff|/|On|/' file

which, if file contains your sample input, produces the output:
Code:
nl-gv-twv-ngeb-ne40e-x8-03_If<1525>|_|202234303|_|On|_|nl-gv-twv-ngeb-ne40e-x8-03(Eth-Trunk10.1236)|_|
nl-gv-twv-ngeb-ne40e-x8-03_If<1526>|_|202234304|_|On|_|nl-gv-twv-ngeb-ne40e-x8-03(Eth-Trunk11.1236)|_|
nl-gv-twv-ngeb-ne40e-x8-03_If<1522>|_|202234308|_|On|_|nl-gv-twv-ngeb-ne40e-x8-03(Eth-Trunk7.1236)|_|
nl-gv-twv-ngeb-ne40e-x8-03_If<1521>|_|202234309|_|On|_|nl-gv-twv-ngeb-ne40e-x8-03(Eth-Trunk4.1236)|_|
nl-gv-twv-ngeb-ne40e-x8-03_If<1518>|_|202234311|_|On|_|nl-gv-twv-ngeb-ne40e-x8-03(GigabitEthernet4/1/0.157)|_|
nl-gv-twv-ngeb-ne40e-x8-03_If<1524>|_|202234312|_|On|_|nl-gv-twv-ngeb-ne40e-x8-03(Eth-Trunk9.1236)|_|
nl-gv-twv-ngeb-ne40e-x8-03_If<1519>|_|202234317|_|On|_|nl-gv-twv-ngeb-ne40e-x8-03(Eth-Trunk1.1236)|_|
nl-gv-twv-ngeb-ne40e-x8-03_If<1523>|_|202234319|_|On|_|nl-gv-twv-ngeb-ne40e-x8-03(Eth-Trunk8.1236)|_|
nl-gv-twv-ngeb-ne40e-x8-03_If<1520>|_|202234323|_|On|_|nl-gv-twv-ngeb-ne40e-x8-03(Eth-Trunk2.1236)|_|
nl-gv-twv-ngeb-ne40e-x8-03_If<6910>|_|202695789|_|On|_|nl-gv-twv-ngeb-ne40e-x8-03(Eth-Trunk17.1143)|_|
nl-gv-twv-ngeb-ne40e-x8-03_If<6877>|_|202695790|_|On|_|nl-gv-twv-ngeb-ne40e-x8-03(Eth-Trunk17.1137)|_|
nl-gv-twv-ngeb-ne40e-x8-03_If<6786>|_|202695791|_|On|_|nl-gv-twv-ngeb-ne40e-x8-03(Eth-Trunk16.1181)|_|
nl-gv-twv-ngeb-ne40e-x8-03_If<6736>|_|202695792|_|On|_|nl-gv-twv-ngeb-ne40e-x8-03(Eth-Trunk16.1167)|_|
nl-gv-twv-ngeb-ne40e-x8-03_If<6842>|_|202695793|_|On|_|nl-gv-twv-ngeb-ne40e-x8-03(Eth-Trunk16.1233)|_|
nl-gv-twv-ngeb-ne40e-x8-03_If<6883>|_|202695794|_|On|_|nl-gv-twv-ngeb-ne40e-x8-03(Eth-Trunk17.1101)|_|
nl-gv-twv-ngeb-ne40e-x8-03_If<6975>|_|202695795|_|On|_|nl-gv-twv-ngeb-ne40e-x8-03(Eth-Trunk17.1116)|_|
nl-gv-twv-ngeb-ne40e-x8-03_If<6815>|_|202695796|_|On|_|nl-gv-twv-ngeb-ne40e-x8-03(Eth-Trunk16.1029)|_|
nl-gv-twv-ngeb-ne40e-x8-03_If<6817>|_|202695797|_|On|_|nl-gv-twv-ngeb-ne40e-x8-03(Eth-Trunk16.1033)|_|
nl-gv-twv-ngeb-ne40e-x8-03_If<6801>|_|202695798|_|On|_|nl-gv-twv-ngeb-ne40e-x8-03(Eth-Trunk16.1164)|_|

the first two lines of which match the output you said you want. If you meant that you just want to process the first two lines in the file and ignore the rest, try:
Code:
sed 's/^Delete_[0-9]*_//;s/|[Oo]ff|/|On|/;3,$d' file

This User Gave Thanks to Don Cragun 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

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies

2. UNIX for Dummies Questions & Answers

Search for a string,delete the line and replace with new string in a file

Hi Everyone, I have a requirement in ksh where i have a set of files in a directory. I need to search each and every file if a particular string is present in the file, delete that line and replace that line with another string expression in the same file. I am very new to unix. Kindly help... (10 Replies)
Discussion started by: Pradhikshan
10 Replies

3. UNIX for Dummies Questions & Answers

How can I replace the lines that start with a star and replace it with numbers start from 1?

I need to replace the (*) in the fist of a list with numbers using sed for example > this file contain a list * linux * computers * labs * questions to >>>> this file contain a list 1. linux 2. computers 3. labs 4. questions (7 Replies)
Discussion started by: aalbazie
7 Replies

4. Shell Programming and Scripting

Remove lines between the start string and end string including start and end string Python

Hi, I am trying to remove lines once a string is found till another string is found including the start string and end string. I want to basically grab all the lines starting with color (closing bracket). PS: The line after the closing bracket for color could be anything (currently 'more').... (1 Reply)
Discussion started by: Dabheeruz
1 Replies

5. UNIX for Dummies Questions & Answers

Appending a character(#) with string search at the start of the line

Hello, I have been browsing through the forum, but unable to find a solution for my requirement. I need to go through a file and search for /home/users and insert a # symbol at the start /home/users. Example output is #/home/users. Can you please help me with the awk or sed command for... (1 Reply)
Discussion started by: chandu123
1 Replies

6. Emergency UNIX and Linux Support

Find a line using a condition and replace a string in that line

Hello, I have a 100 line code. I have given a sample of it below: ABC*654654*1*54.54*21.2*87*1*654654654654 CCC*FS*FS*SFD*DSF GGG*FGH*CGB*FBDFG*FGDG ABC*654654*1*57.84*45.4*88*2*6546546545 CCC*WSF*SG*FGH*GHJ ADA*AF*SFG*DFGH*FGH*FGTH I need to select the line starting with "ABC" its... (3 Replies)
Discussion started by: nithins007
3 Replies

7. Shell Programming and Scripting

Find a line using a condition and replace a string in that line

Hello, I have a 100 line code. I have given a sample of it below: ABC*654654*1*54.54*21.2*87*1*654654654654 CCC*FS*FS*SFD*DSF GGG*FGH*CGB*FBDFG*FGDG ABC*654654*1*57.84*45.4*88*2*6546546545 CCC*WSF*SG*FGH*GHJ ADA*AF*SFG*DFGH*FGH*FGTH I need to select the line starting with "ABC" its... (6 Replies)
Discussion started by: nithins007
6 Replies

8. Solaris

Line too long error Replace string with new line line character

I get a file which has all its content in a single row. The file contains xml data containing 3000 records, but all in a single row, making it difficult for Unix to Process the file. I decided to insert a new line character at all occurrences of a particular string in this file (say replacing... (4 Replies)
Discussion started by: ducati
4 Replies

9. Shell Programming and Scripting

Append string at start of line

Hi, I want to append # at the start of line wherever keyword xyz is found through stream editor? Is it possible? (18 Replies)
Discussion started by: db2cap
18 Replies

10. Shell Programming and Scripting

SED Question: Search and Replace start of line to matching pattern

Hi guys, got a problem here with sed on the command line. If i have a string as below: online xx:wer:xcv: sdf:/asdf/http:https-asdfd How can i match the pattern "http:" and replace the start of the string to the pattern with null? I tried the following but it doesn't work: ... (3 Replies)
Discussion started by: DrivesMeCrazy
3 Replies
Login or Register to Ask a Question