printing only columns containing a determined character with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting printing only columns containing a determined character with awk
# 1  
Old 06-19-2012
Question printing only columns containing a determined character with awk

Hello

I'm trying to extract two columns from a database using awk, the thing is i have a variable number of columns in each row, and I just need to get out the two first ones ending in ","

This is an example:
Code:
ABE, ABE V149 MAZIE ARD CYN ACY, ACY, , , ,TEC, , 5000, , , 1, ZNY,ZDC
ABE, ABE FJC V149 LHY ALB, ALB, , , , TEC, , 8000, , ,1 ,ZNY, ZBW

As you can see the number of elements between the two one I need is not allways the same. I thought maybe with a for (for all the rows ) and an if (to selec for the column) I may be able to get it, but it's getting quite complicated for my level of awk .....I couldn't find any example for this kind of construction (or maybe I dind't understand it, hehe)
¿is there another better way to do it?
¿should I use awk for this?

thank you very much in any case Smilie

Last edited by Franklin52; 06-20-2012 at 03:36 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 06-19-2012
Does this do it?

Code:
awk -F , ' { print $1 "," a[split($2, a, " ")]; } ' input-file >output-file

Output will be one pair per line, separated by a comma.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add character to specific columns using sed or awk and make it a permanent change

Hi, I am writing a shell script where I want that # should be added in all those lines as the first character where the pattern matches. file has lot of functions defined a.sh #!/bin/bash fn a { beautiful evening sunny day } fn b { } fn c { hello world .its a beautiful day ... (12 Replies)
Discussion started by: ashima jain
12 Replies

2. Shell Programming and Scripting

awk pattern match not printing desired columns

Hi all, I'm trying to match the following two files with the code below: awk -F, 'NR==FNR {a=$0; next} ($12,$4) in a {print $12,$1,a}' OFS="," file4.csv file3.csv but the code does not print the entire row from file4 in addition to column 12 and 1 of file3. file4: o,c,q,co,ov,b... (1 Reply)
Discussion started by: bkane3
1 Replies

3. Shell Programming and Scripting

Character printing with AWK

I was wondering if I could use AWK to print from the nth character position to nth for all records in the file. All I have been doing is printing/copying fields rather than characters. I found a way to print lines greater/less/equal to a character length which led me to believe there is a way... (3 Replies)
Discussion started by: MIA651
3 Replies

4. Shell Programming and Scripting

awk based script to ignore all columns from a file which contains character strings

Hello All, I have a .CSV file where I expect all numeric data in all the columns other than column headers. But sometimes I get the files (result of statistics computation by other persons) like below( sample data) SNO,Data1,Data2,Data3 1,2,3,4 2,3,4,SOME STRING 3,4,Inf,5 4,5,4,4 I... (9 Replies)
Discussion started by: ks_reddy
9 Replies

5. Shell Programming and Scripting

Printing in columns

is there a short one-liner that can print out in columns instead of a long list? as in, instead of: apples oranges cats dogs sky monkey giraffe cups spoons tv cable it'll print something like this (properly indented of course :) ): (9 Replies)
Discussion started by: SkySmart
9 Replies

6. Shell Programming and Scripting

Awk while-loop printing extra character

Hi, I'm using a while-loop in an awk script. If it matches a regular expression, it prints a line. Unfortunately, each line that is printed in this loop is followed by an extra character, "1". While-statement extracted from my script: getline temp; while (temp ~ /.* x .*/) print temp... (3 Replies)
Discussion started by: redbluefish
3 Replies

7. Shell Programming and Scripting

Printing columns

Im using awk to print columns. Basically I have a file with like 500 columns and I want to print the 200th-300th column and ignore the rest... how would I do it without putting $200, $201 .... $300 thanks (6 Replies)
Discussion started by: kylle345
6 Replies

8. Shell Programming and Scripting

printing two lines in awk as two columns in excel

hi guys, i would like to print two lines from a file as two adjacent columns using excel using awk.. i have this so far: awk '{for(i=1; i<=NF; i++) {printf("%s\n",$i)}}' "$count".ttt > "$count".csv #this to print the first line from the .ttt file as rows of the first column in the .csv... (9 Replies)
Discussion started by: npatwardhan
9 Replies

9. UNIX for Dummies Questions & Answers

awk printing all columns after (but including) $n

I am piping an "ls -l" to awk so that all it returns is the file size, date, and file name. The problem is that some files may have spaces in the name so awk is only printing the first word in the file name. I won't know how many space-delimited words are in the filename, so what I want to do is... (2 Replies)
Discussion started by: cassj
2 Replies

10. Shell Programming and Scripting

printing columns

Here's what I wrote: #!/bin/sh d1=`grep Dialtone dialtone | awk '{print $2, $3, $4, $5, $6, $7, $8, $9}'` d2=`grep pstsys dialtone | awk '{print $12}'` echo "$d1 $d2" I expected the result to be this: Dialtone on host 1 slot 13 port 1, pstsys05 Dialtone on host 1 slot 13 port 1,... (3 Replies)
Discussion started by: cdunavent
3 Replies
Login or Register to Ask a Question