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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pass variable as insert with SED. 3

Status
Not open for further replies.

AnotherAlan

Technical User
Joined
Feb 10, 2006
Messages
362
Location
GB
Hi all,

Is it possible to insert more than one string with sed.

i.e Something like;
sed '$i\
`cat /tmp/test/ng_pass` ' /tmp/test/pass_test > /tmp/test/pass_test.updated

I would like to insert the contents of a file / variable.
In this case the file ng_pass contains 2 lines;
UK
US
I have dynamic entries and the contents of ng_pass may vary from 1 to 4 different lines that need inserting.
At the moment I have tried with files and variables but cannot get the syntax correct.

Any help appreciated.
Thanks
Alan
 
Hi Feherke,

Thanks for the reply and the solution.
I have a small query.
The file ng_pass is built on the fly after certain conditions have been met earlier in the script and will therefore always apply when it gets to this part.

I am confused as to this part of your code:
/insert condition/ - and how to make it work for me.

Thanks again.
 
Hi

Oops, I misread your code. So you want the second file before the last line.
Code:
awk 'NR>1{print s}{s=$0}END{while(getline<"/insertable/file")print;print s}' /input/file

Feherke.
 
Hi Feherke,

I apologise for keeping you busy.
This doesn't seem to do what I was expecting.

I think I may have explained my problem poorly.
I am automating the roll-out of NIS.
To do this I need to build a list of groups who are allowed access. This list is built as a file, ng_pass, and will contain entries such as admin, support, tsg e.t.c

I would like to then insert, before the last line, each item in the file as a seperate line in the password and shadow files.

The last line of the file is +:x:::::/bin/false

I would like all new entries to precede this line;

Expected output would be;
admin
support
tsg
+:x:::::/bin/false

I can workaround this problem by adding the last line (which never changes) to the ng_pass file, deleting the last line form the password file and just append.
I was trying to be a little too clever for my own good.

Thanks for your help with this, I do appreciate it.
Alan
 
Hi

Then let us take some example :
Code:
[blue]master #[/blue] cat [green][i]pass_test[/i][/green]
1
2
3
4
last

[blue]master #[/blue] cat [green][i]ng_pass[/i][/green]
one
two
three

[blue]master #[/blue] awk 'NR>1{print s}{s=$0}END{while(getline<"[green][i]ng_pass[/i][/green]")print;print s}' [green][i]pass_test[/i][/green]
1
2
3
4
one
two
three
last
The content of ng_pass is before the last line of pass_test. Where am I wrong ?

Feherke.
 
Thanks Feherke.

You are most definately NOT wrong.
I am running Solaris9, changed awk for nawk and good to go.

Much appreciated as always.

Alan
 
Unless you like the possibility of endless loop, replace this:
while(getline<"ng_pass")
with this:
while((getline<"ng_pass")>1)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi

Ahh. Then was the [tt]getline[/tt]. I should copy a couple of times PHV's portable solutions. He had a comment on such issue just a few days ago... [banghead]

Feherke.
 
Thank you Gentlemen.

One day I will get to grips with this awk /sed syntax.
I've read the o'reilly book, cover to cover, but it just doesn't stick.

Anyway, appreciated as always.
Alan
 
For a sed/subshell solution I found:
Code:
sed "\$i\\
$(sed '$!s/$/\\/' ng_pass)
" pass_test

Cheers,
ND [smile]
 
Nice work ND, works like a dream.

Thanks for the effort.
Alan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top