- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- change 5th line after a pattern - 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
Forums
Discussions
Discussions
Discussions
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
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
10-01-2009 07:03 AM
10-01-2009 07:03 AM
I am able to print the mode line (awk's default action). How can I assign this 5th line to a variable so as to change its value and then print it i.e print:
mode = XXX
I am looking for an awk solution.
file:
/usr/sbin/bootpd:/
type =
class =
owner =
group =
mode =
checksum =
size =
$ awk '!--c;/\/usr\/sbin\/bootpd:/ { c=5 }' file
mode =
Solved! Go to Solution.
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2009 07:28 AM
10-01-2009 07:28 AM
Re: change 5th line after a pattern - AWK
And why are you searching for the 5th line under the /usr/sbin/bootpd" pattern? Can't you search for the "mode" pattern and set it? Are there different types of "mode=" lines that are not related to the /usr/sbin/bootpd line?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2009 08:00 AM
10-01-2009 08:00 AM
Re: change 5th line after a pattern - AWK
#!/bin/sh
charset=us-ascii
export charset
find /apps/sci/loaded -type f -mtime 0 -print > /apps/sci/loaded/scifiles
for FILE in /apps/sci/loaded/scifiles
do
cat $FILE |\
while read line
do
SCI=`sed -n 3,3p $line | awk '/046/ && /TOWER/ {print $0}' $line`
if [ "$SCI" != "" ]
then
if [ "$line" = "/apps/sci/loaded/scifiles" ]
then
echo "$line" > /dev/null
elif [ "$line" = "/apps/sci/loaded/mailedtosci.list" ]
then
echo "$line" > /dev/null
elif [ "$line" = "/apps/sci/loaded/mailsci.sh" ]
then
echo "$line" > /dev/null
else
mailx -s "Decoder File `basename $line`" garlric@exchange1 < $line
echo "`basename $line` `date +'%b %d %Y %X'`" >> mailedtosci.list
fi
fi
done
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2009 08:24 AM
10-01-2009 08:24 AM
Re: change 5th line after a pattern - AWK
>> I am looking for an awk solution.
Hmm, why? Apparently you do not know awk too well, or at least not yet. Sorry. Couldn't resist!
Anyway.... assuming you want the line with 'mode=' and assuming you want the xxx to be a variable and assuming you want to stick it back into a file, you may want to try:
# awk -v mode=TEST '/^mode/{$0 = $0 mode} 1' x > y
This reads the file x,
writes to y,
sets up a variable called mode to value TEST.
The passes an awak 'one-liner'.
The one-liner first looks for a line starting with 'mode, and if it finds that ads the variable 'mode' to the end.
The next instruction is '1' which is true and causes awk to do the default thing... print $0.
hth,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2009 08:36 AM
10-01-2009 08:36 AM
Re: change 5th line after a pattern - AWK
And please clarify what you mean with 'after a pattern'.
Could the same line be found earlier?
Could the same line be found later?
So maybe you want something along the lines of the one liner below.
If only starts looking for mode when a 'begin' has been set by looking for the bootpd line.
It stops looking when it has seen one.
$ cat x
/usr/sbin/too early/
mode =
/usr/sbin/bootpd:/
type =
class =
owner =
group =
mode =
checksum =
size =
/usr/sbin/too late/
mode =
checksum =
$ awk -v mode=TEST '/bootpd:/{beg++} beg && ! end && /^mode/ {$0 = $0 mode; end++} 1' x
/usr/sbin/too early/
mode =
/usr/sbin/bootpd:/
type =
class =
owner =
group =
mode =TEST
checksum =
size =
/usr/sbin/too late/
mode =
checksum =
Enjoy!
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2009 08:37 AM
10-01-2009 08:37 AM
Re: change 5th line after a pattern - AWK
Rather interesting.
# awk '!--c {print $0,"XXX"};/\/usr\/sbin\/bootpd:/ { c=5 }' file
mode = XXX
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2009 08:25 PM
10-01-2009 08:25 PM
Re: change 5th line after a pattern - AWK
/usr/sbin/ch_rc -a -p "mode=99" file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2009 12:12 AM
10-02-2009 12:12 AM
Re: change 5th line after a pattern - AWK
attribute = value
I was merely trying to change the value of one attribute (mode) in one stanza (bootpd) leaving all other values/stanza's unchanged.
I think there's enough in the combined response to achieve the task in awk.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2009 03:48 AM
10-02-2009 03:48 AM
Solution> I was merely trying to change the value of one attribute (mode) in one stanza (bootpd) leaving all other values/stanza's unchanged.
Then here's an alternative solution:
# perl -pe '?/usr/sbin/bootpd:?..?mode? and s{(mode =)}{$1 "xxx"}' file
If you want to update "in-place" simply do:
# perl -pi.old -e '?/usr/sbin/bootpd:?..?mode? and s{(mode =)}{$1 "xxx"}' file
...which leaves an unmodified copy of the original file as "file.old".
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2009 05:22 AM
10-02-2009 05:22 AM
Re: change 5th line after a pattern - AWK
I forgot to consider the awk range selector as an option to to solve this. It looks a little cleaner than maintaining flags as I suggested.
For example:
$ awk -v mode=TEST '/bootpd:/,/^mode/ {if (/^mode/) $0="mode = " mode} 1' x
/usr/sbin/too early/
mode =
/usr/sbin/bootpd:/
type =
class =
owner =
group =
mode = TEST
checksum =
size =
/usr/sbin/too late/
mode =
checksum =
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2009 05:42 AM
10-02-2009 05:42 AM
Re: change 5th line after a pattern - AWK
awk 'BEGIN { FS="\n"; RS=""; OFS="\n"; ORS="\n\n" }
$0 !~/^\/usr\/sbin\/bootpd:/ { print }
$1 ~/^\/usr\/sbin\/bootpd:/ {
$6=" mode = r-xr-xr-x"
} ' file
Thank you also Hein for the most recent post. I haven't actually tested it as I was working on an alternative solution and reviewing JRF's perl approach.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2009 05:52 AM
10-02-2009 05:52 AM
Re: change 5th line after a pattern - AWK
Thanks