- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Awk scripting
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
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
06-24-2003 04:03 PM
06-24-2003 04:03 PM
I need to extract the first occurrence of the field ABNA which is located on line 13 of the attached file and rename the file using this field. i.e test.ABNA.file
Could you please assist.
Many thanks
Oktay
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-24-2003 04:38 PM
06-24-2003 04:38 PM
Re: Awk scripting
awk '/ABNA/ {print $0;exit;}' > test.ABNA.file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-24-2003 05:05 PM
06-24-2003 05:05 PM
Re: Awk scripting
The field where ABNA is located is variable and it denotes a company name which changes all the time.
Sorry I should have been more explicit.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-24-2003 05:29 PM
06-24-2003 05:29 PM
Re: Awk scripting
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-24-2003 05:38 PM
06-24-2003 05:38 PM
Re: Awk scripting
From the sample file you posted,I found the the first occurrence of the filed "ABNA" is always exists in the line begins with "BHP".
So, this may help:
fnname=`more test|sed -n '/BHP/p'|awk '{print $8}'`
mv test test.${fnname}.file
Hope it can help you!
-ux
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-24-2003 06:06 PM
06-24-2003 06:06 PM
Re: Awk scripting
fnname=`more test|sed -n '13p`|awk '{print $1}'`
Good luck!
-ux
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-24-2003 07:33 PM
06-24-2003 07:33 PM
Re: Awk scripting
Line 13th is fixed. Is the field 8th in 13th line fixed too?.
In that case you can simply use these two lines.
FIELD=$(sed -n '13p' your_file|awk '{print $8}')
cp your_file test.${FIELD}.file
-Sri
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-24-2003 09:09 PM
06-24-2003 09:09 PM
Re: Awk scripting
Line 13 is fixed however the 8th variable is not and depending on the file it changes?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-24-2003 11:16 PM
06-24-2003 11:16 PM
Re: Awk scripting
If the 8th field in the 13th line is not fixed, how can we isolated the company name?
You must supply some condition to detect this name in order to extract it.
Frank.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2003 01:31 AM
06-25-2003 01:31 AM
Solutiontry this script:
#!/bin/sh
export file_name=`awk '{if (NR==13) {for (i=1;i<=NF;i++) {if($i != ""){print $i; next}}}}' $1`
mv $1 "$file_name"
than you can use it like
#<script_name>
it will rename your file to ABNA
awk will search record number 13 for the first non-empty field(in this case ABNA), then will print this field and exit the for loop.
you can change the last line to
mv $1 test. "$file_name".txt if you like.
Not so short but it works.
Have fun, ionut
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2003 02:19 AM
06-25-2003 02:19 AM
Re: Awk scripting
in the file you attached there is no ABNA string in line 13 but it does appear twice in line 11.
I'm assuming that the string you want to extract is the last non-blank field in line 11.
If this is the case, you can do this:
#!/usr/bin/sh
if [ ! -e $1 ]; then
echo "File $1 does not exist. Exiting"
exit 1
fi
newname=`awk 'NR == 11 { sub("[\t ]+\015$", ""); print $NF}' $1`
[ ! -z $newname ] && mv $1 test.${newname}.file
The awk bit strips the trailing whitespace and CR from line 11 and then prints the last field.
If you want the second last field, change $NF to $(NF - 1)
HTH,
Michael.