- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: Awk field seperator
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
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
тАО01-10-2007 02:58 PM
тАО01-10-2007 02:58 PM
Re: Awk field seperator
the NR doesn't work?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-10-2007 03:05 PM
тАО01-10-2007 03:05 PM
Re: Awk field seperator
Anyhow, I'm still a noob but using NR should be able to do it in a one liner.
I'll have to defer to an awk guru for the one liner... otherwise pipe to awk 'NR>1{exit};1' (sorta like head -1)
awk '/'hell'/ && !/awk/ {print $3}' /tmp/target.txt |awk 'NR>1{exit};1'
-denver
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-10-2007 04:24 PM
тАО01-10-2007 04:24 PM
Re: Awk field seperator
You Unix types... Always seem to think a solution needs a pipe or two to count.
You are in AWK. Stay there.
Let awk do the work!
First step:
whole line: awk '/test/ && (!i++) ' tmp.txt
field: awk '/test/ && (!i++){print $3} ' tmp.txt
Here we test for a piece of string, and if that succeeds test for a variable "i" and increment after the test. If the "i" test is zero, then print.
There is still a 'problem' with this solution. Awk will keep on reading and testing where no more data will ever be produced. So just print and tell it to go home:
Best solution:
awk '/test/ {print $3; exit}' tmp.txt
Enjoy,
Hein van den Heuvel
HvdH Performance Consulting.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-10-2007 05:19 PM
тАО01-10-2007 05:19 PM
Re: Awk field seperator
You da man!!!
Now, how can I get awk to return a value greater than 0 if the string returns null (nothing)?
Also, if I want to accept a file format of 2 fields. The first field is only numbers and second field is a mixed of numbers and characters and to return a value greater than 0 if the format isn't correct?
100100100100 helloworld
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-10-2007 07:49 PM
тАО01-10-2007 07:49 PM
Re: Awk field seperator
I wonder how many question you can out into one single message ...
Your latest one:
>>
Also, if I want to accept a file format of 2 fields. The first field is only numbers and second field is a mixed of numbers and characters and to return a value greater than 0 if the format isn't correct?
100100100100 helloworld
<<
awk 'NF != 2 || $1 !~ /^[0-9]*$/ {exit 1}'
should do it.
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-10-2007 11:30 PM
тАО01-10-2007 11:30 PM
Re: Awk field seperator
If it could be any line, then you need to read on until match or the END. If the END is reached, return other status:
awk '($1 ~ /^[0-9]+$/ && ($2 ~ /^[a-zA-Z0-9]+$/) {print; exit 0} END {exit 1}' tmp.txt
It's time to read that awk man page or book my friend!
hth,
Hein
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-11-2007 12:07 AM
тАО01-11-2007 12:07 AM
Re: Awk field seperator
to clarify the difference of Hein's solution to mine.
My solution aborts at the first line NOT containing twu fields, where the first consists of digits only.
Hein's solution terminates at the first correct entry with status 0; the return value is 1 when not a single correct field was found.
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-11-2007 03:03 AM
тАО01-11-2007 03:03 AM
Re: Awk field seperator
# awk '/hell/ && !/awk/ {print (($3=="")?0:$3);exit}' file
~cheers
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-11-2007 11:38 PM
тАО01-11-2007 11:38 PM
Re: Awk field seperator
- « Previous
- Next »