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
# 8  
Old 03-20-2014
Thanks Shamrock, I'm getting closer!

My problem now is column 1 and 2 are separated by a space! If only I could use your solution and separate 1 and 2 as well and I'll be sorted.

I'll keep digging.

Thanks for all of your help Smilie
# 9  
Old 03-20-2014
Note: not every awk understands the {,} repetition operators, out of the box. mawk does not know them and gawk 3 needs an option to switch them on (--re-interval or --posix). An alternative would be to alternatively use two spaces followed by a + operator.
If we combine that with alternation ( | ), we get:
Code:
awk -F '  +|\t' '{print $2, $3, $4}' file

which, combined with the input from post #3 gives:
Code:
Col2 Col3 Col4
  
host2 OFFLINE host2 is offline and has been for 7 minutes
host1 ONLINE host1 is online

--edit--
If the separator between the first two fields is a single space you could add and exception like this:
Code:
awk -F '  +|\t' '{split($1,F,/[ ]/); print F[2], $2, $3}' file


Last edited by Scrutinizer; 03-20-2014 at 02:01 PM..
# 10  
Old 03-20-2014
Another option could be to get the columns based on spacing, rather than character delimited. For example, in your example data above, you could print the 1st & 4th column like such:
Code:
perl -lne '@m = /(.{14})(.{12})(.{14})(.*)/; print $m[0].$m[3]' file

# 11  
Old 03-20-2014
Quote:
Originally Posted by keenboy100
Thanks Shamrock, I'm getting closer!

My problem now is column 1 and 2 are separated by a space! If only I could use your solution and separate 1 and 2 as well and I'll be sorted.

I'll keep digging.

Thanks for all of your help Smilie
In that case you'd have split up $1 into its sub-fields...
Code:
awk -F"  +" '{printf("%s %s %s\n", a[split($1, a, " ")], $2, $5)}' file


Last edited by shamrock; 03-20-2014 at 03:27 PM.. Reason: codefix
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