Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Awk field sperators

Status
Not open for further replies.

jimmy80

Programmer
May 7, 2003
13
0
0
GB
I was hoping someone might be able to help - I'm trying to extract the 7th column from a file - each field is seperated by a tab - I'm using the following awk command but it doesn't return anything - i don't know has it something to do with the file sperator or what but was hoping someone else might be able to point me in the right direction!
cat testfile.txt | awk 'BEGIN {FS="\t"}{print $7}' | sort -u > test.log

Thanks,
Jimmy
 
Enter the physical TAB character instead of \t.
Code:
cat testfile.txt | awk 'BEGIN {FS="    "}{print $7}' | sort -u > test.log
Greg.
 
Yes I believe tabs have affected your script. If you print $1 it works but every other $ i.e. $2 $3 will fail because of the tabs. You could substitute tabs with spaces using sed, there must be a better way but this will work.
sed 's/:/ /g' fileold > filenew

Now you can Print $7
cat filenew | awk 'BEGIN {FS=" "}{print $7}'
or
cat filenew | awk '{print $7}'
 
And what about this ?
Code:
t=`echo "\t\c"
awk -F"$t" '{print $7}' testfile.txt | sort -u>test.log

Hope This Help
PH.
 
To trace the problem, you may try my suggestion as follows:

First, see the code from the file:
sed -n l testfile.txt
Then you know the actual seperator from the file.

Second, see your seperator setting work or not.
awk -F"\t" '{print NF}' testfile.txt
Then you can know the total fields now.

In your case, I think that the problem is the tab chracter may more than one between the field. You may try:
awk '{FS = "\t*"}{print$3}' textfile.txt | sort -u > test.log
Note: The code FS = "\t*" may only work in some of version of awk. My linux is work but it isn't work in Sun. Because I found that the code is ">" rather than "\t" from the file.

tikual
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top