awk - print columns with text and spaces


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - print columns with text and spaces
# 1  
Old 03-20-2014
awk - print columns with text and spaces

Hi,

I'm using awk to print columns from a tab delimited text file:

Code:
awk '{print " "$2" "$3" $6"}' file

The problem I have is column 6 contains text with spaces etc which means awk only prints the first word.

How can I tell awk to print the whole column content as column 6?

Thanks,
# 2  
Old 03-20-2014
Quote:
Originally Posted by keenboy100
Hi,

I'm using awk to print columns from a tab delimited text file:

Code:
awk '{print " "$2" "$3" $6"}' file

The problem I have is column 6 contains text with spaces etc which means awk only prints the first word.

How can I tell awk to print the whole column content as column 6?

Thanks,
How are your columns separated?

Can you post a sample input?
# 3  
Old 03-20-2014
Hi,

Sorry!

The following sample is similar:

Code:
Col1             Col2         Col3               Col4

09:12:37      host2       OFFLINE       host2 is offline and has been for 7 minutes
09:23:15      host1       ONLINE        host1 is online

Column 4 is the column with the spaces similar to column 6 in my original question.

Thanks,
# 4  
Old 03-20-2014
Quote:
Originally Posted by keenboy100
Hi,

Sorry!

The following sample is similar:

Code:
Col1             Col2         Col3               Col4

09:12:37      host2       OFFLINE       host2 is offline and has been for 7 minutes
09:23:15      host1       ONLINE        host1 is online

Column 4 is the column with the spaces similar to column 6 in my original question.

Thanks,
Hi Keenboy,

I assumed that col1, col2, col3 and col4 are separated by tabs and col4 has spaces.

The following command print one empty column before col1 and after col4

Code:
awk -F"\t" '{print " "$1" "$2" "$3" "$4" "}' file

# 5  
Old 03-20-2014
use awk -F'\t' ... to set the field separator to tabs
# 6  
Old 03-20-2014
Sorry again!

It's a weird file. Column 1 and 2 are separated by a space, 2 and 3 seem to separated by a tab (or 4 spaces)....

It's very inconsistent.

Would it be best replacing all white spaces with a comma except the spaces in the column containing the text and then print with awk? Smilie
# 7  
Old 03-20-2014
You can define your FS to be strictly composed of 2 or more space characters whick will let you yank off the 6th field intact...
Code:
awk -F" {2,}" '{print $2, $3, $6}' file

These 2 Users Gave Thanks to shamrock For This Post:
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 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

3. 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

4. UNIX for Dummies Questions & Answers

AWK print last field including SPACES

I have simple test.sh script, see below: bill_code=`echo $record | awk -F"|" '{print $1}'` Fullname=`echo $record | awk -F"|" '{print $3}'` email=`echo $record | awk -F\ '{print $4}'` The last field contains spaces: see csv below: A0222|Y|DELACRUZ|-cc dell@yahoo.com-cc support@yahoo.com ... (9 Replies)
Discussion started by: quay
9 Replies

5. Shell Programming and Scripting

print columns with spaces

Hi All I've a file which is similar to the one given below column1 coulmn2 column3 column4 A B C D X Y F G H I would like to get it in this format A|B|C|D ||X|Y F||G|H Is... (4 Replies)
Discussion started by: Celvin VK
4 Replies

6. UNIX for Dummies Questions & Answers

Print files with spaces using awk

When I use: find . -ls | awk 'BEGIN { OFS = ";"} {print $1,$2,$11}'I get a nice result, yet the files with spaces in it show only the first word and all other characters after the blank space are not printed. e.g. 'file with a blank space' is printed file 'file_with a blank space' is... (7 Replies)
Discussion started by: dakke
7 Replies

7. UNIX Desktop Questions & Answers

How to print several pages only text without spaces among

1 I open pdf file - it has empty borders of space without text 2 I want to print 9 pages to save paper & eyes 3 In Okular i choose print - 9 pages. And I have no option for adjusting text to text (to print without spaces among 9 pages). What program can you recommend to me for this... (1 Reply)
Discussion started by: Xcislav
1 Replies

8. Shell Programming and Scripting

Print filenames with spaces using awk

Hello all, I want to list the file contents of the directory and number them. I am using la and awk to do it now, #ls |awk '{print NR "." $1}' 1. alpha 2. beta 3. gamma The problem I have is that some files might also have some spaces in the filenames. #ls alpha beta gamma ... (7 Replies)
Discussion started by: grajp002
7 Replies

9. 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

10. Shell Programming and Scripting

How to print arguments along with spaces using awk

Hi All, file_1.txt contains aaa bbbb hhhh vvvvv mmmmm iiiii What i want is to search for the first coloumn of each line using awk.i.e as below: awk '/aaa/ {printf(<>)}' file_1.txt The print part (<>) should contain all the values(or coloumns ) in the particular line(here it... (8 Replies)
Discussion started by: jisha
8 Replies
Login or Register to Ask a Question