Visit The New, Modern Unix Linux Community


[Solved] Inserting a line beginning with regex


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] Inserting a line beginning with regex
# 1  
[Solved] Inserting a line beginning with regex

I have been trying to insert a line after a regex but I can't do it. Here is the code I am using:

Code:
cat saved_doc 
SET type = type1 
SET type = STORE = y
/vol/san_e1
/vol/san_5
/vol/san_e9
/vol/san_e11
/vol/san_e12
/vol/san_e13
/vol/san_e14
/vol/san_e16
/vol/san_e17
/vol/san_e18
/vol/san_e19
/vol/san_e20
/vol/san_e23
/vol/san_e24
/vol/san_e26
/vol/san_e27
/vol/san_e28

What I have done is this:

Code:
cat saved_doc |gawk '{printf "NEW\n"; print}'

This makes the file look good except that I want NEW to be inserted starting after the line SET type = STORE = y only. Then the file should look like this:

Code:
SET type = type1 
SET type = STORE = y
NEW
/vol/san_e1
NEW
/vol/san_5
NEW
/vol/san_e9
NEW
/vol/san_e11
NEW
/vol/san_e12
NEW
/vol/san_e13
NEW
/vol/san_e14
NEW
/vol/san_e16
NEW
/vol/san_e17
NEW
/vol/san_e18
NEW
/vol/san_e19
NEW
/vol/san_e20
NEW
/vol/san_e23
NEW
/vol/san_e24
NEW
/vol/san_e26
NEW
/vol/san_e27

Anybody have an idea? Once I have this fixed, I can place it into the rest of the script but for now NEW starts at the first line of the file and goes to the end. I've tried sed, but that goes for specific lines and
Code:
NR %2 = 0

but that captures even lines and so on.
# 2  
Based on line number you could do:

Code:
awk 'NR>3{print "NEW"}1' infile

or on "STORE = y" string:

Code:
awk 'N{print "NEW"}/STORE = y/{N=1}1' infile

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Code:
awk 'NR>2{$0="NEW" RS $0}1' saved_doc

This User Gave Thanks to rdcwayx For This Post:
# 4  
Thanks to All!

This seemed simple, but I was trying to use sed which I have used most often and got close to but never found the correct awk solution.

Thanks to all!
# 5  
Using sed:
Code:
sed '/STORE = y/,$ s/$/\nNEW/; $ s/\nNEW//' file

or
Code:
sed '/STORE = y/,$ {$ !s/$/\nNEW/}' file


Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #730
Difficulty: Medium
The first version BASIC language was released on 1 May 1964.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Inserting n characters to beginning of line if match

I would like to insert n number of characters at the beginning of each line that starts with a given character. If possible, I would be most appreciative for a sed or awk solution. Given the data below, I would like to be able to insert either 125 spaces or 125 "-" at the beginning of every line... (6 Replies)
Discussion started by: jvoot
6 Replies

2. UNIX for Dummies Questions & Answers

[Solved] Appending beginning of filename to end

Hi Guys, I have serveral directories like this: (2013) blablabla(blabla) - blabla (blabla) or (1997) blablabla(blabla) - blabla (blabla) and have to rename them to something like that: blablabla(blabla) - blabla (blabla) (2013) and blablabla(blabla) - blabla (blabla) (1997) Easy... (2 Replies)
Discussion started by: Nateshift
2 Replies

3. UNIX for Dummies Questions & Answers

sed - Add a variable line to the end of a block beginning with a regex

Hi, Need some help with sed. I have a file that has sections : e.g. a=blah b=blah d=blah e=blah There's many sections in the file. (1 Reply)
Discussion started by: andyatit
1 Replies

4. Shell Programming and Scripting

Commenting a specific line and inserting a new line after commented line.

Hello All, I have following file contents cat file #line=aaaaaa #line=bbbbbb #line=cccccc #line=dddddd line=eeeeee #comment=11111 #comment=22222 #comment=33333 #comment=44444 comment=55555 Testing script Good Luck! I would like to comment line line=eeeeee and insert a new line... (19 Replies)
Discussion started by: manishdivs
19 Replies

5. Shell Programming and Scripting

[Solved] SED - Bash - Inserting multi Tab character in the command

Hello. I am using : sed -i -e '/§name_script§/a#'"${MY_TAB11}"'# \ #'"${MY_TAB1}"'The Standard way'"${MY_TAB7}"'# \ #'"${MY_TAB1}"'==============='"${MY_TAB7}"'# \ ' "$CUR_FILE" Is there a better way to define "MY_TAB7","MY_TAB11" in other way than : MY_TAB1=$'\t' MY_TAB2=${MY_TAB1}$'\t'... (2 Replies)
Discussion started by: jcdole
2 Replies

6. UNIX for Dummies Questions & Answers

vim copy line and paste at the beginning, middle, and end of another line

How would you do vim copy line and paste at the beginning, middle, and end of another line. I know yy copies the whole line and p pastes the whole line, but on its own separate line. Sometimes I would like to copy a line to the beginning, middle, or end of another line. I would think this would be... (3 Replies)
Discussion started by: cokedude
3 Replies

7. UNIX for Dummies Questions & Answers

How to specify beginning-of-line/end-of-line characters inside a regex range

How can I specify special meaning characters like ^ or $ inside a regex range. e.g Suppose I want to search for a string that either starts with '|' character or begins with start-of-line character. I tried the following but it does not work: sed 's/\(\)/<do something here>/g' file1 ... (3 Replies)
Discussion started by: jawsnnn
3 Replies

8. UNIX for Dummies Questions & Answers

Regex for beginning of line until a comma

What is a regex for "the dalai lama, his holiness the" that would just grab "the dalai lama" and one that would just grab "his holiness the"? Both should exclude the comma.. I was trying '^.*' and many variants with no luck. (6 Replies)
Discussion started by: glev2005
6 Replies

9. Shell Programming and Scripting

perl regex help solved

Hi there I have a list of mac addresses that are in this format 0100237D2136XX 0118A905599EXX 0118A9054E15XX 010003BAF1F3XX 0118A905599EXX 0100237D6522XX 010C53BAF1F3XX 010003BAF1F3XX 010C53BAF1F3XX The 01 is at the beginning has been put on by my software, but the rest is the mac... (0 Replies)
Discussion started by: rethink
0 Replies

10. Shell Programming and Scripting

Unix Script with line number at beginning of each line.

Could anybody help me. I need to create a script that reads a text file from STDIN and prints out the file to STDOUT with line numbers at the beginning of each line. Thanks. (5 Replies)
Discussion started by: mascorro
5 Replies

Featured Tech Videos