Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with data insert 1

Status
Not open for further replies.

alan147

Technical User
Nov 15, 2002
128
GB
Good morning

I am trying to update a configuration from from a script. I need to be able to insert 4 rows of data 2 rows below a specific word.

I.e my existing file is something like

# December
line1
line2

this will need to become

# December
line1
line2
new row 1
new row 2
new row 3
new row 4

This is not simply a case of appending the 4 new rows of data to the end of the file.

Any ideas?

Thanks

Alan
 
And what have you tried so far ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
I have tried using sed to serach for string and then append a single line at that point. I haven't yet been able to get the line appended two rows below the sersch string. The data in line 1 nad 2 have a great many similarities. Is it possible to search for two strings which would uniquely identify the point at which the insert should be made?

Thanks

Alan
 
Hi

Then my [tt]sed[/tt] is better then your [tt]sed[/tt]... ;-)
Code:
[gray]# original data :[/gray]
[blue]master #[/blue] seq 3
1
2
3

[gray]# insert more then one line after the line containing '2' :[/gray]
[blue]master #[/blue] seq 3 | sed '/2/aone\
two\
three'
1
2
one
two
three
3
Tested with GNU [tt]sed[/tt]. But other [tt]sed[/tt] versions can also insert multiple lines.

Feherke.
 
Thanks, the script works well but in my file I have something like this:

# December's Household rates and the sections & endorsement service
#HH Q /home/ssu/engines/Dec house 7121 <ip address> 20051601
#HH D /home/ssu/engines/Dec eo2terms 7122 <ip address> 20061201

# Some more configuration

I am trying to insert data for January for example so that the file looks like this

# December's Household rates and the sections & endorsement service
HH Q /home/ssu/engines/Dec house 7121 <ip address> 20061201
HH D /home/ssu/engines/Dec eo2terms 7122 <ip address> 20061201
#
# January's Household rates and the sections & endorsement service
HH Q /home/ssu/engines/Jan house 7011 <ip address> 20070101
HH D /home/ssu/engines/Jan eo2terms 7012 <ip address> 20070101

#Some more configuration.

The white space between the various elements are TABS.

Alan
 
Hi

So you do not want to insert immediately after the line containing "December", but after its section. So find "December", skip it and skip the subsequent lines until the first empty line, then insert the new section ?
Code:
[blue]master #[/blue] cat monthfile.txt
# November
HH
HH

# December
HH
HH

# Whenever
HH
HH

[blue]master #[/blue] sed '/December/,/^$/{/^$/a# January\
HH\
HH\


}' monthfile.txt
# November
HH
HH

# December
HH
HH

# January
HH
HH

# Whenever
HH
HH
Tested with GNU [tt]sed[/tt]. Unix [tt]sed[/tt] versions probably needs minor modification.

Feherke.
 
Thanks, it works well. I have a couple of other questions.

Is it possible to use variables in the sed command to build the string for example:

HH Q /home/ssu/engines/$new_month house 7121 <ip address> $new_year0101

and, how do I escape the ' in /a# January's Household

Thanks

Alan
 
I have put double quotes around the command which allows me to use variables but the it ignores the new line.

Alan
 
Hi

Good try with the double quotes ( " ), but you should continue with the tries and double the backslashes ( \ ) too.
Code:
sed "/December/,/^$/{/^$/a# January\[red]\[/red]
HH $new_year0101\[red]\[/red]
HH\[red]\[/red]


}" monthfile.txt

Feherke.
 
Great, it does just what is needed.

Thanks feherke for all your help.

Alan
 
Do remind me. What are those little purple stars for again?

Alan Bennett said:
I don't mind people who aren't what they seem. I just wish they'd make their mind up.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top