need help with awk in printing the fields in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting need help with awk in printing the fields in a file
# 1  
Old 10-06-2008
need help with awk in printing the fields in a file

hi all

i get a file from a server and i do not know how many fields that file will contain.
i need to cut from the second column of the file to the last, irrespective of how many fields are there.

is there any way to make the awk command dynamic for fetching from the second to the last field in the file, some thing similar to cut commmand where giving -f2- will give fields from second to the end..
the reason why i cannot use cut is that a few of the fields have tab as a delimiter and a few have space, as awk treats both as same and fetches the field irrespective of the delimiter being tab or space, i want it to be an awk command...

any help.
# 2  
Old 10-06-2008

Code:
awk '{sub( /^[^ \t]+[ \t]+/,""); print}' FILENAME

# 3  
Old 10-07-2008
thank you very much ...... one more help ..can you please explain the command ?
# 4  
Old 10-07-2008
i am getting the following error. can you please help?

> awk '{sub( /^[^ \t]+[ \t]+/,""); print}' 21CP
+ awk {sub( /^[^ \t]+[ \t]+/,""); print} 21CP
awk: syntax error near line 1
awk: illegal statement near line 1
# 5  
Old 10-07-2008
Quote:
Originally Posted by sais
i am getting the following error. can you please help?

> awk '{sub( /^[^ \t]+[ \t]+/,""); print}' 21CP
+ awk {sub( /^[^ \t]+[ \t]+/,""); print} 21CP
awk: syntax error near line 1
awk: illegal statement near line 1
Use nawk or /usr/xpg4/bin/awk on Solaris.

Quote:
Originally Posted by sais
can you please explain the command ?
Code:
sub(/^[^ \t]+[ \t]+/,"")

The sub command replaces the first field and the tabs or spaces after the first field with nothing ("").

^[^ \t]+
-> select one or more non spaces/tabs from the begin of the line

[ \t]+ -> select one or more spaces/tabs after the characters of the begin of the line

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk to clean up input file, printing both fields

In the f1 file below I am trying to clean it up removing lines the have _tn_ in them. Next, removing the characters in $2 before the ninth /. Then I remove the ID_(digit- always 4). Finally, the charcters after and including the first _. It is curently doing most of it but the cut is removing $1... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. UNIX for Beginners Questions & Answers

Continued trouble matching fields in different files and selective field printing ([g]awk)

I apologize in advance, but I continue to have trouble searching for matches between two files and then printing portions of each to output in awk and would very much appreciate some help. I have data as follows: File1 PS012,002 PRQ 0 1 1 17 1 0 -1 3 2 1 2 -1 ... (7 Replies)
Discussion started by: jvoot
7 Replies

3. Shell Programming and Scripting

awk Selective printing of fields

Hi Gurus, I have following input file. I tried multiple awk combinations to print selected columns without success. HEX ID Name ver FLRGT Start Time Total Shared End Date ----- -------- --- ------ ------------------------ -------------- -------... (4 Replies)
Discussion started by: shunya
4 Replies

4. Shell Programming and Scripting

awk sort based on difference of fields and print all fields

Hi I have a file as below <field1> <field2> <field3> ... <field_num1> <field_num2> Trying to sort based on difference of <field_num1> and <field_num2> in desceding order and print all fields. I tried this and it doesn't sort on the difference field .. Appreciate your help. cat... (9 Replies)
Discussion started by: newstart
9 Replies

5. Shell Programming and Scripting

How to print 1st field and last 2 fields together and the rest of the fields after it using awk?

Hi experts, I need to print the first field first then last two fields should come next and then i need to print rest of the fields. Input : a1,abc,jsd,fhf,fkk,b1,b2 a2,acb,dfg,ghj,b3,c4 a3,djf,wdjg,fkg,dff,ggk,d4,d5 Expected output: a1,b1,b2,abc,jsd,fhf,fkk... (6 Replies)
Discussion started by: 100bees
6 Replies

6. Shell Programming and Scripting

How to automate printing all fields twice?

Hi, I'm looking for a clever way to print all columns of a matrix twice using a for loop and NF I think. So instead of awk -F "," '{print $1 " " $1 " " $2 " " $2...} I can do something like awk -F "," '{for (i,i<=NF,i++) (print $i... So I get something like A1,A2,A3 B1,B2,B3... (2 Replies)
Discussion started by: flotsam
2 Replies

7. Shell Programming and Scripting

Printing First Three Fields

Hello, I have several files in the following format: 1*2,3,4,5 And I wish to only print the first three fields, including the special characters '*' and ','. I have used awk to print fields before, but I do not know how to do it to include the delimiters, and how you can do it with... (1 Reply)
Discussion started by: Jahn
1 Replies

8. Shell Programming and Scripting

awk sed cut? to rearrange random number of fields into 3 fields

I'm working on formatting some attendance data to meet a vendors requirements to upload to their system. With some help on the forums here, I have the data close. But they've since changed what they want. The vendor wants me to submit three fields to them. Field 1 is the studentid field,... (4 Replies)
Discussion started by: axo959
4 Replies

9. Shell Programming and Scripting

AWK - printing certain fields when field order changes in data file

I'm hoping someone can help me on this. I have a data file that greatly simplified might look like this: sec;src;dst;proto 421;10.10.10.1;10.10.10.2;tcp 426;10.10.10.3;10.10.10.4;udp 442;10.10.10.5;10.10.10.6;tcp sec;src;fac;dst;proto 521;10.10.10.1;ab;10.10.10.2;tcp... (3 Replies)
Discussion started by: eric4
3 Replies

10. Shell Programming and Scripting

printing select fields in awk

Hi, I want to print certain fields from my data file depending on certain conditions. Somebody pls let me know how to send it to awk. The command below is the one which I want to use in a shell script and this prints fine cat ./datafile.dat | grep -i $SEARCH_STR | awk -F: '{ print $1 $2 $3... (5 Replies)
Discussion started by: maverix
5 Replies
Login or Register to Ask a Question