Operating System - Linux
1748089 Members
5083 Online
108758 Solutions
New Discussion юеВ

Re: replace a specifk line in a file with sed or awk

 
SOLVED
Go to solution
Simon Jespersen
Frequent Advisor

replace a specifk line in a file with sed or awk

Hey!
I want to replace line nr 220 in a file with the text "my newline"
How can that be done with se or awk.
9 REPLIES 9
James R. Ferguson
Acclaimed Contributor
Solution

Re: replace a specifk line in a file with sed or awk

Hi Simon:

# sed -e '220s/.*/my newline/' file

Regards!

...JRF...
Hein van den Heuvel
Honored Contributor

Re: replace a specifk line in a file with sed or awk

awk '{if (NR==220) $0="my newline";print}' old > new


hth,
Hein.
Simon Jespersen
Frequent Advisor

Re: replace a specifk line in a file with sed or awk

Thanks for thje reply, but i get strange happening

with the sed line it just behave like im doing a cat on the file
# sed -e '2s/.*/mynewline/' passwd_sijes

Here im trying to replace line 2 in the file im just getting all the file directet to stdout
a71223:
password = SyNHeFTvoiMGw
lastupdate = 1135680886
flags = ADMCHG

a74040:
password = SyNHeFTvoiMGw
lastupdate = 1157718667
flags = ADMCHG

a05243:
password = SyNHeFTvoiMGw
lastupdate = 1157718669
flags = ADMCHG

a57659:
password = SyNHeFTvoiMGw
lastupdate = 1157718670
flags = ADMCHG


can you se why this is happening.
Simon Jespersen
Frequent Advisor

Re: replace a specifk line in a file with sed or awk

How to i use an ksh shell var with awk and sed.
i have the line number in an variable that i want to parse to the awk program. Se $lineNr
cat myfile | awk '{if (NR==$lineNr) $0="my newline";print}' old > new

rhanks
James R. Ferguson
Acclaimed Contributor

Re: replace a specifk line in a file with sed or awk

Hi (again) Simon:

You must redirect the STDOUT of 'sed' to a new file:

# sed -e '220s/.*/my newline/' file file.new
# mv file.new file

If you would like to do "inplace" updates without having to move the output back over the original file, use Perl:

# perl -pi.old -e 's/.*/mynewline/ if $.==220' file

This will modify line # 220, replacing it with "mynewline". The 'file" will be modified inplace with a backup copy named 'file.old'. If you don't want the backup, do:

# perl -pi -e 's/.*/mynewline/ if $.==220' file

Regards!

...JRF...
spex
Honored Contributor

Re: replace a specifk line in a file with sed or awk

Hello,

> How to i use an ksh shell var with awk and sed.

$ LN=2
$ awk -v lineNr=${LN} '{if (NR==lineNr) $0="my newline";print}' < old > new1
$ sed "${LN}s/.*/myline/" < old > new2

PCS
James R. Ferguson
Acclaimed Contributor

Re: replace a specifk line in a file with sed or awk

Hi (again):

> How to i use an ksh shell var with awk and sed.i have the line number in an variable that i want to parse to the awk program. Se $lineNr

cat myfile | awk '{if (NR==$lineNr) $0="my newline";print}' old > new

You pass variables to 'awk' using the '-v' switch and argument:

# awk -v lineNr=220 '{if (NR==lineNr) $0="my newline";print}' file file.new

*Notice* that the 'lineNr' variable does not have the sigil ($) when referenced within the 'awk' script, nor does it have it when it is declared. However:

# lineNR=220
# awk -v lineNr=${lineNr} '{if (NR==lineNr) $0="my newline";print}' file file.new

...is necessary since you are now using a shell variable's value.

Do not use 'cat' to open and read a file and then pipe that output to 'awk'! That's a waste of a process. 'awk' uses the commandline arguments as filenames to process.

For 'sed' use double quotes like this:

# L=220
# sed -e "${L}s/.*/mynewline/" file > file.new
# mv file.new file

Regards!

...JRF...
Simon Jespersen
Frequent Advisor

Re: replace a specifk line in a file with sed or awk

cool stuff, thanks a lot.
Peter Nikitka
Honored Contributor

Re: replace a specifk line in a file with sed or awk

Hi,

one addition to JRF's sed lines:
Keep in mind, when putting such stuff in a script, to check the success of the commands.

Being not root or acting via NFS or ..., you may have write access to the directory but no read access the the file or vice versa, so something like
# L=220
# sed -e "${L}s/.*/mynewline/" file > file.new
# mv file.new file

may destroy 'file'. I suggest always
sed -e "${L}s/.*/mynewline/" file > file.new && mv file.new file

which else won't try a 'mv' when 'file.new' cannot be created.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"