- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Match a field and print the line
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
11-29-2004 12:23 PM
11-29-2004 12:23 PM
Match a field and print the line
I have a file with about 10000 lines. I have to match the 8th field of each line with a value (say "11") and output the line to another file if the field matches the value. Each field in the file is delimited with "|". I tried using
cat filename|awk -F "|" '{ if ($8 == 11 ) {getline; print $0}}
and also
cat filename | while read line
do
val=`cat -d"|" -f8 $line`
if [ $val -eq 11 ] ; then
echo $line
fi
done
Both did not work.
Can anyone please help me doing this.
Thanks,
Anand.
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2004 12:35 PM
11-29-2004 12:35 PM
Re: Match a field and print the line
-f8 $line"? Or is that just a typo in your post?
You would want: val=`cut -d"|" -f8 $line`
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2004 01:47 PM
11-29-2004 01:47 PM
Re: Match a field and print the line
awk -F '|' '{if ($8 == "11") print $0}' infilename > outfilename
If you want to evaluate $8 as an integer you need to coerce the type and change the comparison to
if (($8 + 0) == 11)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2004 01:54 PM
11-29-2004 01:54 PM
Re: Match a field and print the line
$cat test | awk -F "|" '$8 == 11 {print $0}'
or
val=$(cat -d "|" -f8 $line)
There are spaces in the field separators.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2004 02:15 PM
11-29-2004 02:15 PM
Re: Match a field and print the line
cat filename |
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2004 03:10 PM
11-29-2004 03:10 PM
Re: Match a field and print the line
awk -F "|" '{ if ( $8 == 11) print $0 }'
Problem in your try is, you have matched one line's 8th element but print NEXT line there.
In next try,
try as,
while read line; do
val=`echo $line | cut -d"|" -f8`
if [ $val -eq 11 ] ; then
echo $line
fi
done <
HTH.
- Tags:
- while loop