Insert line based on found string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert line based on found string
# 1  
Old 09-07-2016
Insert line based on found string

Hi All
I'm trying to insert a new line at the before each comment line in a file.
Comment lines start with '#-----'
there are other comments with in lines but I don't want a new line there.
Example file:
Code:
blah
blah #do not insert here
#this is a comment
blah #some more 
#another comment

the desired result would be
Code:
blah
blah #do not insert here

#this is a comment
blah #some more 

#another comment

I tried
Code:
tr '#' '\012'

and a number of variants using sed etc but I don't get the desired result.

grateful for any pointers . . .
# 2  
Old 09-07-2016
Hello Mudshark,

Could you please try following and let me know if this helps you.
Code:
awk '/^#/{print RS $0;next} 1'   Input_file

Output will be as follows.
Code:
blah
blah #do not insert here

#this is a comment
blah #some more 

#another comment

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 09-07-2016
And in sed :
Code:
 sed '/^#/{x;p;x;}' input-file

These 2 Users Gave Thanks to greet_sed For This Post:
# 4  
Old 09-07-2016
Many thanks all for the responses.
RavinderSingh13 - That worked ok although I had to use nawk on Solaris.
greet_sed - that also worked fine.
I also found another similar thread and used this ... ( where filename = 'x' and comment lines start with '#-')
Code:
nawk '{ gsub ("#-", "\n#-") }1' x

# 5  
Old 09-07-2016
Try also
Code:
sed 's/^#/\n#/' file

This User Gave Thanks to RudiC For This Post:
# 6  
Old 09-08-2016
Unix sed needs backslash-realnewline i.e. needs multi-line
Code:
sed 's/^#/\
#/' file

Or insert an empty line with
Code:
sed '/^#/i\
' file

# 7  
Old 09-08-2016
Code:
awk '/^#/{print x}1' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to insert missing string based on pattern in file

Using the file below, which will always have the first indicated by the digit after the - and last id in it, indicated by the digit after the -, I am trying to use awk to print the missing line or lines in file following the pattern of the previous line. For example, in the file below the next... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Insert String every n lines, resetting line counter at desired string

I need to read a text file and insert a string every n lines, but also have the line counter restart when I come across a header string. Line repeating working every 3 lines using code: sed '0~3 s/$/\nINSERT/g' < INPUT/PATH/FILE_NAME.txt > OUTPUT/PATH/FILE_NAME.txt I cannot seem to find... (1 Reply)
Discussion started by: Skonectthedots
1 Replies

3. UNIX for Dummies Questions & Answers

insert string at end of line if it found from list

Hi all, can some one help me out file 1 i have 06/01 3:14 d378299 06/01 8:10 d642036 06/01 10:51 d600441 06/01 10:52 d600441 06/01 11:11 d607339 06/01 11:49 d398706 something like this and in file named list i have ( there is space btwn 06/01 and 11:49 and d398706) d607339... (5 Replies)
Discussion started by: zozoo
5 Replies

4. Shell Programming and Scripting

Insert specific line when found similar value

Hi All, I have file like this: file1: 3778 10474 24 3778 10475 24 3778 10476 25 3778 10495 26 3794 10001 33 3794 10002 33 3794 10004 33 3794 10007 34 3794 10008 34 3794 10011 34 3794 10012 34 3794 10013 34 3794 10017 34 3810 10282 27 (6 Replies)
Discussion started by: attila
6 Replies

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

6. Shell Programming and Scripting

Grep a string and write a value to next line of found string

Hi, I have two variables x and y. i need to find a particular string in a file, a workflow name and then insert the values of x and y into the next lines of the workflow name. basically it is like as below wf_xxxxxx $$a= $$b= $$c= figo $$d=bentley i need to grep the 'wf_xxxx' and then... (6 Replies)
Discussion started by: angel12345
6 Replies

7. Shell Programming and Scripting

grep on string and printing line after until another string has been found

Hello Everyone, I just started scripting this week. I have no background in programming or scripting. I'm working on a script to grep for a variable in a log file Heres what the log file looks like. The x's are all random clutter xxxxxxxxxxxxxxxxxxxxx START: xxxxxxxxxxxx... (7 Replies)
Discussion started by: rxc23816
7 Replies

8. Shell Programming and Scripting

Insert blank line if grep not found

Hi all, I've googling around forum regarding my prob, the nearest would same as thread tittled Insert blank line if grep not found, but she/he did not mention the solution, so I would like to request your help I've this task, to search in file2 based on pattern in file1 and output it to... (4 Replies)
Discussion started by: masterpiece
4 Replies

9. Shell Programming and Scripting

insert new line at found chars

Hey gang, I have: XXZZXXZZXX 123 asdaffggh dfghyrgr ertyhdhh XXZZXXZZXX 234 sdg XXZZXXZZXX 456 gfg fggfd That is all on one line. Very simply put I want to do is something like: sed s'/XXZZXXZZXX /\n/g' or tr 'XXZZXXZZXX ' '/n' I have tried various things but can never get the desired... (6 Replies)
Discussion started by: crowman
6 Replies

10. Shell Programming and Scripting

Insert blank line if grep not found

Hello everyone... please help if you can -- I'm stumped. Making this work will save me hours of manual labor: I need to search file2 for pattern in file1. If pattern found append file2 line to file3. If pattern not found append a blank line to file3. file1 contents example: 123 456 789... (6 Replies)
Discussion started by: michieka
6 Replies
Login or Register to Ask a Question