Append string at start of line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Append string at start of line
# 15  
Old 04-05-2011
Thank you Bakunin.
Vgersh, your second option was helpful; it does the job in a single command though it is not difficult or consumes time to redirect and then rename. Thank you all :-)

---------- Post updated at 08:01 AM ---------- Previous update was at 08:00 AM ----------

Vgersh,
Sorry to say, but the the first option is still not helpful.
# 16  
Old 04-05-2011
Code:
printf '/hotbackup/s!^!#!g\n1,$~\nwq!\n' | ex -s myFile

# 17  
Old 04-05-2011
Code:
printf '/hotbackup/s!^!#!g\n1,$~\nwq!\n' | ex -s myFile

This is appending # on every line Smilie

Code:
printf '/hotbackup/s!^!#!g\nwq!\n' | ex -s myFile

This is working but not appending globally. It is only working on the line where it first finds the keyword and does not consider the rest of the file.
Sorry for I did not see it before as the file was huge and I cross checked the end page of it
# 18  
Old 04-05-2011
The problem with vgersh99's ex scripts is that they seem to assume an implicit i/o loop, much like sed, which reads in each line and then executes the script for each one. That's not the case. '/hotbackup/s/regexp/repltext/g\nwq\n' will search for the next instance of a line with 'hotbackup', make substitutions on that line only, then write and quit. To mark multiple lines for processing, the global command is needed. Perhaps the following is what is sought:

Code:
printf '%s\n' g/hotbackup/s/^/#/ wq | ex -s myFile

or with ed:
Code:
printf '%s\n' g/hotbackup/s/^/#/ w q | ed -s myFile

Regards,
Alister

Last edited by alister; 04-05-2011 at 11:34 AM..
This User Gave Thanks to alister For This Post:
# 19  
Old 04-06-2011
Thank you Alister, second command works. Again, the first command append # on every line ;-)
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. Shell Programming and Scripting

Append this string to end of each line

Platform: Solaris 10 I have a file like below $ cat languages.txt Spanish Norwegian English Persian German Portugese Chinese Korean Hindi Malayalam Bengali Italian Greek Arabic I want to append the string " is a great language" at end of each line in this file. (3 Replies)
Discussion started by: omega3
3 Replies

3. UNIX for Dummies Questions & Answers

Append a string on the next line after a pattern string is found

Right now, my code is: s/Secondary Ins./Secondary Ins.\ 1/g It's adding a 1 as soon as it finds Secondary Ins. Primary Ins.: MEDICARE B DMERC Secondary Ins. 1: CONTINENTAL LIFE INS What I really want to achieve is having a 1 added on the next line that contain "Secondary Ins." It... (4 Replies)
Discussion started by: newbeee
4 Replies

4. Shell Programming and Scripting

Append string to first line of a file

Hi, Please suggest me to write unix command, HEADER20110101 string append to first line of a file.. Regards Akshu (3 Replies)
Discussion started by: akshu.agni
3 Replies

5. UNIX for Dummies Questions & Answers

Append symbol at the start of each line

hi, i have some values in excel sheet as in below format: 122 144 222 555 666 etc.... i need to get the output in the below manner.. £122 £144 £222 £555 £666 (1 Reply)
Discussion started by: arunmanas
1 Replies

6. Shell Programming and Scripting

how to append a string to next line in perl

hi all , i have a requirement like this.. this just a sample script... $ cat test.sh #!/bin/bash perl -e ' open(IN,"addrss"); open(out,">>addrss"); @newval; while (<IN>) { @col_val=split(/:/); if ($.==1) { for($i=0;$i<=$#col_val;$i++) { ... (2 Replies)
Discussion started by: tprayush
2 Replies

7. Shell Programming and Scripting

Append a string at the end of every line in a file

Hi Friends, I have a file with many lines as shown below. /START SAMPLE LINE/ M:\mmarimut_v6.4.0_pit_01\java\build.xml@@\main\v6.4.0_pit_a M:\mmarimut_v6.4.0_pit_01\port\Post.java@@\main\v6.4.0_pit_a M:\mmarimut_v6.4.0_pit_01\switchview\View.java@@\main\v6.4.0_pit_a /END SAMPLE LINE/ I... (1 Reply)
Discussion started by: nmattam
1 Replies

8. Shell Programming and Scripting

How to append a string to a specific location in a line

Hi, I have a to modify a line and insert a keyword in the middle to a specific location. My line looks like this FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '"' TRAILING NULLCOLS (ABC, DEF, GHI) I want to change it as FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '"' TRAILING... (4 Replies)
Discussion started by: mwrg
4 Replies

9. UNIX for Advanced & Expert Users

append the line with the previous if it not start with 1=

How to append the line with the previous if it not start with 1=. 1=ttt, 2=xxxxxx, 3=4545 44545, 4=66666, 1=ttt, 2=xxxxxx, 3=34434 3545, 4=66666, 5=ffffff 6=uuuuuuu, 7=ooooooo 1=ttt, 2=xxxxxx, 3=311343545, 4=66666 1=ttt, 2=xxxxxx, 5=XAXAXA, 7=FDFD (3 Replies)
Discussion started by: palsevlohit_123
3 Replies

10. Shell Programming and Scripting

append string with spaces to a line

hi i have a file like (every string contains 16 chars) CTL1330000000000 0000 00 008000 0080000000 i need to form a line and write to a file CTL13300000000000000 00008000 0080000000 total chars should be 64 ... (2 Replies)
Discussion started by: Satyak
2 Replies
Login or Register to Ask a Question