Insert line based on found string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert line based on found string
# 8  
Old 09-08-2016
Quote:
Originally Posted by greet_sed
And in sed :
Code:
 sed '/^#/{x;p;x;}' input-file

Hi greet_sed,
Could you please explain how below part is working.
Code:
{x;p;x;}

---------- Post updated at 01:01 PM ---------- Previous update was at 12:55 PM ----------

Quote:
Originally Posted by Scrutinizer
Code:
awk '/^#/{print x}1' file

Hi Scrutinizer,
How print x is printing a new line , and when i keep blank like below
HTML Code:
awk '/^#/{print }1' file
it prints comment line twice. Please explain.

Last edited by looney; 09-08-2016 at 03:17 PM.. Reason: forgot to remove x in print
# 9  
Old 09-08-2016
Quote:
Originally Posted by looney
Hi Scrutinizer,
How print x is printing a new line , and when i keep blank like below
HTML Code:
awk '/^#/{print x}1' file
it prints comment line twice. Please explain.
Hello looney,

So when you do print xit will print actually a NULL value(which is in other words will be a new line only because there is NO value for vriable named x here). But when we do print, it will definetly print line starting with # 2 times because you have given command print without mentioning any line eg-->$0or any variable like print x so by default it will print the current line(which offcourse is the line which is starting from #), then it will print it again because we have mentioned 1 there.
So awkworks on method of condition{action} and by writing 1here we are making the condition part as TRUE so action should occure here but we haven't mentioned any action to be done so by default action which is print will print the current line(which is again line starting with #), so that is why print xprints a new line(value of variable x which is NULL and then line's value which starts with #) and print} 1part prints line 2 times because both of the times we are giving instructions to awkto print the line starting with # as explained above.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 10  
Old 09-08-2016
Hi,

Hope it helps.

Quote:
If an expression matches a line that starts with #, group of commands {x;p;x} is executed . First, line in pattern space is exchanged to hold space by x command. Next, p prints the pattern space . Current pattern space is empty (due to exchange by x) followed by \n.
Next line in hold space is exchanged with pattern space by x. As all commands are executed, it prints the pattern space which contains the line ( line starts with # );
Note that, when sed reads a line it removes the newline character and when it prints it adds it back.
# 11  
Old 09-09-2016
And just for the ex of it try the script below whose only pros is that the file is modified in place...
Code:
ex -s file <<eof
g/^#/i|
.
x
eof

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