Removing columns from awk '{ print $0 }'


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing columns from awk '{ print $0 }'
# 1  
Old 04-17-2011
Removing columns from awk '{ print $0 }'

I have a one-line command,
Code:
lsusb | awk '{ $1=""; $2=""; $3=""; $4=""; $5=""; $6=""; print $0 }'

It works, and gives the results I expect, I was just wondering if I am missing some easier way to nullify the first 6 column variables?

Something like,
Code:
lsusb | awk '{ $(1-6)=""; print $0 }'

But of course, the above doesn't work, and gives the error:
Code:
awk: (FILENAME=- FNR=1) fatal: attempt to access field -5

Any hints?
# 2  
Old 04-17-2011
Well, you could use a for loop:[code]awk ' { for(i=1;i
# 3  
Old 04-17-2011
@ Chubler_XL: I got this to work, also.
Code:
lsusb | awk '{ for (i=1; i<7; i++) $i=""} { print $0 }'

# 4  
Old 04-22-2011
Or:
Code:
awk '{sub(".*" $6 FS,x)}1' file

# 5  
Old 04-22-2011
Quote:
Originally Posted by Franklin52
Code:
awk '{sub(".*" $6 FS,x)}1' file

I like that just a little better, because I get less whitespace in front of the output. Although, (at least for me, it is harder to understand the syntax)

Could you explain?

I would like to keep the ability to modify the number of columns removed from 6, to anything I need.
# 6  
Old 04-22-2011
Quote:
Originally Posted by Franklin52
Or:
Code:
awk '{sub(".*" $6 FS,x)}1' file

Hmmm. In my opinion, that's a dangerous construct and a few caveats are in order.

Since the first argument to sub() is an extended regular expression, any regular expression metacharacters in $6 could yield an unexpected result.

Also, if one of the fields which follows $6 is identical to $6 or contains $6 at its end, the greedy .* will extend beyond $6.

Just a heads up in case either of those conditions could arise.

Regards,
Alister
# 7  
Old 04-22-2011
OK, I noticed, although slightly different from my OP, that using Franklin52's code, this works:
Code:
lsusb | awk '{sub(".*" $6 FS,x)}1'

But,
Code:
ls *.txt | awk '{sub(".*" $6 FS,x)}1'

does NOT work if there are files that have spaces in the filename.

EDIT: Nevermind I solved it with:
Code:
ls -l *.txt | awk '{sub(".*" $8 FS,x)}1'


Last edited by AlphaLexman; 04-22-2011 at 09:34 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to use "awk" to print columns from different files in separate columns?

Hi, I'm trying to copy and paste the sixth column from a bunch of files into a single file having each column pasted in separate columns (and not one after each other in just one column.) I tried this code but works only partially because it copied and pasted 50 rows of each column... (6 Replies)
Discussion started by: Frastra
6 Replies

2. Shell Programming and Scripting

awk print columns and variable

Hi, Can anyone help with the below please? I have written some code which takes an input file, and and prints the contents out to a new file - it then loops round and prints the same columns, but increments the ID column by 1 each time. Input file; NAME,1,15-Dec-15, NAME,1,21-Dec-15,... (9 Replies)
Discussion started by: Ads89
9 Replies

3. Shell Programming and Scripting

awk print columns which are not null

I am working on a file with several columns as below MO_NAME,FAULT_TYPE,CLASS,CODE1,CODE2,CODE3 RXOCF-101,BTS INTERNAL,FAULT CODES CLASS 2A,53,58 RXOCF-101,BTS INTERNAL,FAULT CODES CLASS 2B,24 RXOCF-101,BTS INTERNAL,FAULT CODES CLASS 2A,33 RXOCF-101,BTS INTERNAL,FAULT CODES CLASS 2D,57 ... (12 Replies)
Discussion started by: Rizwan Rasul
12 Replies

4. Shell Programming and Scripting

awk print string with removing all spaces

Hi all, I want to set 10 set of strings into a variable where: removing all spaces within each string change the delimiter from "|" to "," Currently, I've the below script like this:Table=`ten character strings with spaces in-between and each string with delimiter "|" | tr -d ' ' |... (7 Replies)
Discussion started by: o1283c
7 Replies

5. Shell Programming and Scripting

Awk: is it possible to print into multiple columns?

Hi guys, I have hundreds file like this, here I only show two of them: file 1 feco4_s_BB95.log ZE_1=-1717.5206260 feco4_t_BB95.log ZE_1=-1717.5169250 feco5_s_BB95.log ZE_1=-1830.9322060... (11 Replies)
Discussion started by: liuzhencc
11 Replies

6. Shell Programming and Scripting

Print unique records in 2 columns using awk

Is it possible to print the records that has only 1 value in 2nd column. Ex: input awex1 1 awex1 2 awex1 3 assww 1 ader34 1 ader34 2 output assww 1 (5 Replies)
Discussion started by: quincyjones
5 Replies

7. Shell Programming and Scripting

Removing columns using awk

HI , I have a comma delimiter file, in which I want to remove 8th and 9th column. I tried removing those columns using the below code awk 'BEGIN { FS=","; OFS="," } {$8=$9="";gsub(",+",",",$0)}1' infile But the problem is 8th and 9th columns are user entered fields, theyvhave carriage... (1 Reply)
Discussion started by: mora
1 Replies

8. Shell Programming and Scripting

Removing columns using awk

HI , I want to remove 5th and 6th column from a .csv file using awk.is there any way of this apart from writing the each field as below awk -F, '{print $1,$2,$3,$7......$100}' OFS=, infile. Thx, Deepti (4 Replies)
Discussion started by: gaur.deepti
4 Replies

9. Shell Programming and Scripting

using awk to print some columns of a file

Hi, i have a file with content 00:01:20.613 'integer32' 459254 00:01:34.158 459556 00:01:36.626 'integer32' 459255 and i want to print only output as below 00:01:20.613 459254 00:01:34.158 459556 00:01:36.626 459255 i dont want the word 'integer32' which is the second column. i... (2 Replies)
Discussion started by: dealerso
2 Replies

10. Shell Programming and Scripting

cannot print the columns i want with awk.

hi friends! i have a script where a execute a veritas command, available_media wich retrieves me a list of tapes .lst then i execute cat /tmp/listtapes.lst | grep -v VL |sed '/^$/d'|awk -F, '{print $1, $3, $4, $9} ' > /tmp/media1.lst but it prints all the columns instead of the four... (3 Replies)
Discussion started by: pabloli150
3 Replies
Login or Register to Ask a Question