- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- replace a specifk line in a file with sed or awk
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-13-2007 12:44 AM
тАО06-13-2007 12:44 AM
I want to replace line nr 220 in a file with the text "my newline"
How can that be done with se or awk.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-13-2007 12:49 AM
- Tags:
- sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-13-2007 12:51 AM
тАО06-13-2007 12:51 AM
Re: replace a specifk line in a file with sed or awk
hth,
Hein.
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-13-2007 01:01 AM
тАО06-13-2007 01:01 AM
Re: replace a specifk line in a file with sed or awk
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-13-2007 01:05 AM
тАО06-13-2007 01:05 AM
Re: replace a specifk line in a file with sed or awk
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-13-2007 01:14 AM
тАО06-13-2007 01:14 AM
Re: replace a specifk line in a file with sed or awk
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...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-13-2007 01:23 AM
тАО06-13-2007 01:23 AM
Re: replace a specifk line in a file with sed or awk
> 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
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-13-2007 01:24 AM
тАО06-13-2007 01:24 AM
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
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...
- Tags:
- evil cat
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-13-2007 01:24 AM
тАО06-13-2007 01:24 AM
Re: replace a specifk line in a file with sed or awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-13-2007 10:53 PM
тАО06-13-2007 10:53 PM
Re: replace a specifk line in a file with sed or awk
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