- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Replace two lines with one using AWK
Operating System - Linux
1823077
Members
3464
Online
109645
Solutions
Forums
Categories
Company
Local Language
юдл
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Forums
Discussions
юдл
back
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
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- 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
тАО01-09-2005 05:46 PM
тАО01-09-2005 05:46 PM
Hi
I need to replace two lines with one. I tried SED but looks like SED does not accept Ctrl chars as input (eg. linefeed) as required for replacing the two lines.
I need to scan text files and replace the following:
#ifdef ABC
#123456
with
# Test
There is a carriage return between the #ifdef and the #123456.
Have tried with awk without any luck - can someone suggest how to do this.
Thanks in advance
JUP
I need to replace two lines with one. I tried SED but looks like SED does not accept Ctrl chars as input (eg. linefeed) as required for replacing the two lines.
I need to scan text files and replace the following:
#ifdef ABC
#123456
with
# Test
There is a carriage return between the #ifdef and the #123456.
Have tried with awk without any luck - can someone suggest how to do this.
Thanks in advance
JUP
Solved! Go to Solution.
3 REPLIES 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-09-2005 06:30 PM
тАО01-09-2005 06:30 PM
Re: Replace two lines with one using AWK
Hi,
sed -e 's/#ifdef ABC//g' -e 's/#123456/# Test/g' yourfilename
NiCK
sed -e 's/#ifdef ABC//g' -e 's/#123456/# Test/g' yourfilename
NiCK
just for fun
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-10-2005 09:07 AM
тАО01-10-2005 09:07 AM
Re: Replace two lines with one using AWK
Thanks Nick
however your SED command leaves a blank line in the file. In total I wish to replace 4 lines with 1 so I will have 3 blank lines.
The only way to do this is with AWK - I have come up with this command but its not working. It is:
awk '{gsub("#ifdef ABC"\r\n#123456, "# Test"); print $0}' file.h > file.out
Does anyone know whats going wrong here ?
Thanks in advance
however your SED command leaves a blank line in the file. In total I wish to replace 4 lines with 1 so I will have 3 blank lines.
The only way to do this is with AWK - I have come up with this command but its not working. It is:
awk '{gsub("#ifdef ABC"\r\n#123456, "# Test"); print $0}' file.h > file.out
Does anyone know whats going wrong here ?
Thanks in advance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-10-2005 12:56 PM
тАО01-10-2005 12:56 PM
Solution
Awk is literally 'line-by-line'. It can't pattern match over multiple lines.
Perl can (although in my quick tests, I couldn't get it to work (phooey)), so this leaves doing it the hard way.
awk '
END {
print THIS
}
{
LAST=THIS
THIS=$0
if (SKIP == 1) {
SKIP=0
next
}
if ((LAST == "#ifdef ABC") && (THIS == "#123456")) {
print "#Test"
SKIP=1
} else {
print LAST
}
}' < yourfile
I'm sure there's a neater, shorter way out there though.
Perl can (although in my quick tests, I couldn't get it to work (phooey)), so this leaves doing it the hard way.
awk '
END {
print THIS
}
{
LAST=THIS
THIS=$0
if (SKIP == 1) {
SKIP=0
next
}
if ((LAST == "#ifdef ABC") && (THIS == "#123456")) {
print "#Test"
SKIP=1
} else {
print LAST
}
}' < yourfile
I'm sure there's a neater, shorter way out there though.
One long-haired git at your service...
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
Company
Learn About
News and Events
Support
© Copyright 2025 Hewlett Packard Enterprise Development LP