- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- 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
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
01-07-2007 04:43 PM
01-07-2007 04:43 PM
echo hello1 192.168.1.9|awk 'BEGIN { FS = "." } ; { print $1"."$2"."$3}'
hello1 192.168.1
I only need to pring the first three octats from the second field and delete the first field (hello1).
The echo string is an output from a loop.
Thanks
Solved! Go to Solution.
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2007 05:01 PM
01-07-2007 05:01 PM
Re: Awk field seperator
I have some non awk code that I was given on ITRC that does this job.
IP=`expr "${LINE}" : ".*[^0-9]\([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\).*"`
The expression above grabs the first three octets of an ip address.
It doesn't use awk though.
SEP
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Tags:
- expr
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2007 05:11 PM
01-07-2007 05:11 PM
Re: Awk field seperator
OCT1=`echo $IP | awk -F. '{print $1}'`
OCT2=`echo $IP | awk -F. '{print $2}'`
OCT3=`echo $IP | awk -F. '{print $3}'`
IPB=`echo $OCT1.$OCT2.$OCT3`
After what I posted.
Sorry for splitting the post, it was not intentional. Please provide me the slapping I so richly deserve in the point assignment phase(e.g. don't assign to both posts).
Thanks.
SEP
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2007 05:20 PM
01-07-2007 05:20 PM
Re: Awk field seperator
10 points for the right answer.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2007 08:34 PM
01-07-2007 08:34 PM
Re: Awk field seperator
echo hello1 192.168.1.9 | awk 'BEGIN { FS = "." } ; { print substr($1,8,11)"."$2"."$3}'
result
192.168.1
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2007 08:49 PM
01-07-2007 08:49 PM
Re: Awk field seperator
echo hello1 192.168.1.9 | awk 'BEGIN { FS = "." } ; { print substr($1,8,10)"."$2"."$3}'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2007 09:24 PM
01-07-2007 09:24 PM
Re: Awk field seperator
echo hello1 192.168.1.9 | awk '{print $2}' |
awk -F"." '{print $1"."$2"."$3}'
output should be:
192.168.1
rgds
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2007 10:52 PM
01-07-2007 10:52 PM
Re: Awk field seperator
- Tags:
- cut
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2007 11:11 PM
01-07-2007 11:11 PM
Re: Awk field seperator
because the first field is 'hello1 192' in respect to the filed delimiter '.', you get what awk was told to do.
echo hello1 192.168.1.9|awk '{split($2,a,"."); printf("%s.%s.%s\n" a[1],a[2],a[3])}'
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2007 11:56 PM
01-07-2007 11:56 PM
Re: Awk field seperator
# echo hello1 192.168.1.9|awk -F"[ .]" 'BEGIN{OFS="."};{print $2,$3,$4>
192.168.1
Note that the input field separator is a character class consisting of a space (blank) or a dot (".").
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-08-2007 12:01 AM
01-08-2007 12:01 AM
Re: Awk field seperator
Change awk's field separator regex to recognize both . and space.
$ echo hello1 192.168.1.9 | awk -F'[. ]' '{print $2"."$3"."$4}'
192.168.1
PCS
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-08-2007 01:50 AM
01-08-2007 01:50 AM
Re: Awk field seperator
I would have just piped through 2 awk statements - forgot about regex.
IE:
echo hello1 192.168.1.9 | awk '{print $2}' | awk -F. '{print $1"."$2"."$3}'
Rgds...Geoff
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-08-2007 02:20 AM
01-08-2007 02:20 AM
Re: Awk field seperator
Well, it appears that Spex and I agree on the solution :-))
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-08-2007 04:49 AM
01-08-2007 04:49 AM
SolutionBut you could take the default whitespace field seperator and substitute a period followed by numbers at the end of $2 with nothing:
awk '{sub (/.[0-9]+$/,"",$2); print $2}'
Or you can 'match' the first 3 groups of numbers seperated by a period and print that.
That would allow this to work in free formatted text, not just on 'the second field'.
awk '{match($0,/[0-9]+\.[0-9]+\.[0-9]+/); print substr($0,RSTART,RLENGTH)}'
Enjoy,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-08-2007 09:24 PM
01-08-2007 09:24 PM
Re: Awk field seperator
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2007 12:20 PM
01-09-2007 12:20 PM
Re: Awk field seperator
Have you guys tried to double quote the echo string?
By default the -F,FS and OFS are a space character so if you put the echo string in double quote, it preserves the white space and therefore mess it up if add spaces. Try it.
Hein has provided the best solution so far!!!
Open for debate.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2007 08:58 PM
01-09-2007 08:58 PM
Re: Awk field seperator
the result of
echo a b | prog
echo 'a b' | prog
echo "a b" | prog
or
prog $(echo a b)
...
will be the same. In the view of the 'echo' command, it's different - in the first case it sees two parameters, in the other one parameter. But the result is the same: 'echo' will send the two parameters to its output, seperated by a space - which is exactly the same in case 2 or 3, where the space is taken from the original input of the single parameter.
Different will be a scenario like this:
echo a
echo 'a
Only in the first case the result would NOT contain a TAB, but a space!
mfG Peter
- Tags:
- echo
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2007 01:58 PM
01-10-2007 01:58 PM
Re: Awk field seperator
I would like to get only the first line from the output? (whatever the output might be).
awk '/'hell'/ && !/awk/ {print
$3}' /tmp/target.txt
HH
HH
HH
HH
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2007 02:19 PM
01-10-2007 02:19 PM
Re: Awk field seperator
awk '/'hell'/ && !/awk/ {print $2}' /tmp/target.txt |head -1
-denver
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2007 02:32 PM
01-10-2007 02:32 PM
Re: Awk field seperator
awk 'NR>1{exit}/'hell'/ && !/awk/ {print $3}' /tmp/target.txt
-denver
- 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