- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Quick awk question
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
02-06-2006 03:34 AM
02-06-2006 03:34 AM
aaa ab abc abd abcde abds # space is tab
and a script
#!/usr/bin/sh
for user in $(awk -F"\t" '{ print $2 }' input)
do
echo $user
done
but the output is
ab
abc
abd
abcde
abds
when I run it from the command line it comes out the way I want it
ab abc abd abcde abds
Two questions
1. Why does the format change?
2. How do I fix it? I thought it was a path issue but it seems that is not the case.
3. $(awk -F"\t" '{ print $2 }' input | sort)
doest not work in the script either but it works from the command line.
Thanks
Richard
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2006 03:48 AM
02-06-2006 03:48 AM
Re: Quick awk question
?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2006 03:50 AM
02-06-2006 03:50 AM
Re: Quick awk question
$2 is ab - that is all that should be printed....
Rgds...Geoff
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2006 03:53 AM
02-06-2006 03:53 AM
Re: Quick awk question
I can't test it for you b/c I can't get that awk expression to work at all. But assuming that it works (you got output in your example), the easiest way I know to make multiline output "collect" into a single line is to define the whole thing as a function, see below.
#!/usr/bin/sh
awkit()
{
for user in $(awk -F"\t" '{ print $2 }' input)
do
echo $user
done
}
awkit $1 $2 $3 $4 $5 $6 $7 $8 $9
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2006 03:55 AM
02-06-2006 03:55 AM
Re: Quick awk question
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2006 03:56 AM
02-06-2006 03:56 AM
Re: Quick awk question
Since you didn't indicate what you wanted it to do I don't know how to tell you to fix it.
I suspect the file "input" is not exactly as you described. Please post this output:
od -Ad -tc -v input
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2006 04:08 AM
02-06-2006 04:08 AM
Re: Quick awk question
Richard,
Is your input file a single line with tab-seperated columns?
Or does it have multipel lines.
I have the feeling you need something along the lines of:
awk '{i=0; while (i++ < NF) {print $i}}' input | sort
hth,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2006 05:00 AM
02-06-2006 05:00 AM
Re: Quick awk question
FIle tab-seperated
sedi6s sedi6a sedi6s ssdi0s zabcd9a abcder1
sedi6a sedi6z sedi6a ssdi0d zabcd9x abcder9
# od -Ad -tc -v input
0000000 s e d i 6 s \t s e d i 6 a s e
0000016 d i 6 s s s d i 0 s z a b c
0000032 d 9 a a b c d e r 1 \n s e d i
0000048 6 a \t s e d i 6 z s e d i 6 a
0000064 s s d i 0 d z a b c d 9 x
0000080 a b c d e r 9 \n
0000088
This is the output from the command line
#awk -F"\t" '{ print $2 }' input
sedi6a sedi6s ssdi0s zabcd9a abcder1
sedi6z sedi6a ssdi0d zabcd9x abcder9
and would like the same format out of the script.
Thanks
Richard
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2006 05:02 AM
02-06-2006 05:02 AM
Re: Quick awk question
FIle tab-seperated
sedi6s\t sedi6a sedi6s ssdi0s zabcd9a abcder1
sedi6a\t sedi6z sedi6a ssdi0d zabcd9x abcder9
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2006 05:09 AM
02-06-2006 05:09 AM
Re: Quick awk question
# awk -F"\t" '{ print $2 }' file
sedi6a
sedi6z
# awk '{ print $2 }' file
sedi6a
sedi6z
Rgds...Geoff
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2006 05:18 AM
02-06-2006 05:18 AM
Re: Quick awk question
So, basically, you want to print out the rest of the line - after the first tab...
Rgds...Geoff
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2006 05:20 AM
02-06-2006 05:20 AM
Re: Quick awk question
OK. I seems that you have specified a tab character as your shell's IFS.
If I do:
# IFS=
# for user in $(awk -F"\t" '{ print $2 }' input)
> do
> echo $user
> done
Using your *last* file descriptino, I get your output exactly as you descibe at commandline, but not in the script.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2006 05:21 AM
02-06-2006 05:21 AM
Re: Quick awk question
# cat file.awk
#!/usr/bin/sh
awk -F"\t" '{ print $2 }' input
Rgds...Geoff
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2006 05:26 AM
02-06-2006 05:26 AM
Re: Quick awk question
The "for" loop in script is parsing the input (from awk) at each whitespace.
Replace the "for" loop with something like
user=$(awk -F\t '{print $2}' input)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2006 05:27 AM
02-06-2006 05:27 AM
Re: Quick awk question
Looks like a confusion between who gets to pick up the tab as seperator: the shell or awk.
You might want to make that explicit by bringing it into the awk program:
awk 'BEGIN {FS="\t"} { print $2 }' input
or
awk 'BEGIN {FS="\011"} { print $2 }' input
For next time...
1) - That 'od' output helped a lot.
2) - just attach sample input, tabs and all, as a .TXT file.
Cheers,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2006 08:17 AM
02-06-2006 08:17 AM
Re: Quick awk question
awk -F"\t" '{print $2}' input | sort
or
cut -f 2 input | sort
should get it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2006 08:33 AM
02-06-2006 08:33 AM
Solutionawk -F "\t" '{print $2}' input | while read user
do
echo "${user}"
done
The Shell's read command will load any remianing values into the last variable. In your case, since you are reading just 1 variable, all of the remaining tokens on the input line are loaded into the single variable, user.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2006 09:10 AM
02-06-2006 09:10 AM
Re: Quick awk question
Thanks
Richard