Awk, ouptut columns in a file by column name


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Awk, ouptut columns in a file by column name
# 1  
Old 01-06-2012
Awk, ouptut columns in a file by column name

Hi,
i am posting this after not finding proper solution in any other discussion threads

I have a text data file with the first row as the column header names.
The file can be comma, tab, space, pipeline, .... delimited

Example of a pipeline delimited:
file1.txt
Code:
colA|colB|colC|colD|colE|colF
1|4|k2|5w|5|7
9|d|78|5|2|9
98|ad|9s|23|0d
j9|3|s9|dw|8k|i

If i call a script say script1.sh and pass the input data delimiter, output delimiter, ColC and ColF as parameters,
it should only display data for those two columns and in the desired format

Note that we should be able to pass any number of column names to the script.
For our example i am using only 2 columns
I don't want to pass column position as parameter because the order of columns can change in the next data file

Code:
cat file1.txt | script1.sh "|" "," ColC ColF

Here "|" means it's the delimiter for the input data, "," is the delimiter to be used for output, the remaining parameters are the column names

The output should be:
Code:
k2,7
78,9
ad,0d
s9,i

It's okay not to print the column header in the output
Any help on how to write script1.sh is greatly appreciated

Thanks,
-srinivas y.
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 01-06-2012 at 04:00 PM.. Reason: code tags, please!
# 2  
Old 01-06-2012
Why would you write something that awk already does ?

Do not reinvent the wheel ! Smilie


Example: to print col4 and col7 if the delimiter of your input is a | and you want the output field separator to be a <tab> :

Code:
awk -F"|" '{print $4 OFS $7}' OFS="\t" yourfile

# 3  
Old 01-06-2012
how about this thread for starters.
# 4  
Old 01-06-2012
another quick example with column 1 and 3 of a test file :
Code:
$ cat tst
1|abc|2
2|def|4
3|ghi|6
4|jkl|8
5|mno|10

Code:
$ awk -F"|" '{print $1 OFS $3}' OFS="\t" tst
1    2
2    4
3    6
4    8
5    10

Code:
$ awk -F"|" '{print $1 OFS $3}' OFS=":" tst
1:2
2:4
3:6
4:8
5:10

# 5  
Old 01-06-2012
ctsgnb, your example has hardcoded column position
I want to able to pass column name as it appears in the column header as parameter
'colD' need not be in the 4position next time.
I think i should have chosen better column names to avoid confusion

---------- Post updated at 02:19 PM ---------- Previous update was at 02:15 PM ----------

vgersh99, thanks but the provided link has an example to search only one column and for a space delimited file
I would like to pass multiple and provide the input and output delimiter

Thanks
# 6  
Old 01-06-2012
Quote:
Originally Posted by ysrini
vgersh99, thanks but the provided link has an example to search only one column and for a space delimited file
I would like to pass multiple and provide the input and output delimiter

Thanks
You can take the quoted thread as a 'starter/skeleton' and adjust for any additional functionality.
# 7  
Old 01-06-2012
Not sure the scripting language is adapted fo doing such things. Of course it is still possible but more tedious than a select into a Database.

Put your data into a database then
Code:
select colA, colB from yourtable;

there is also a bunch of formatting option available in SQL,SQL+,Pl/Sql so that you can make the rendering fit with your expectations.

Last edited by ctsgnb; 01-06-2012 at 03:32 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

Using awk to split a column into two columns

Hi, I am trying to split the following output into two columns, where each column has Source: Destination: OUTPUT TO FILTER $ tshark -r Capture_without_mtr.pcap -V | awk '/ (Source|Destination): /' | more Source: x.x.x.x Destination: x.x.x.x Source:... (2 Replies)
Discussion started by: sand1234
2 Replies

2. Shell Programming and Scripting

awk split columns to row after N number of column

I want to split this with every 5 or 50 depend on how much data the file will have. And remove the comma on the end Source file will have 001,0002,0003,004,005,0006,0007,007A,007B,007C,007E,007F,008A,008C Need Output from every 5 tab and remove the comma from end of each row ... (4 Replies)
Discussion started by: ranjancom2000
4 Replies

3. Shell Programming and Scripting

awk add all columns if column 1 name matches

Hi - I want to add all columns if column1 name matches. TOPIC1 5 1 4 TOPIC2 3 2 1 TOPIC3 7 2 5 TOPIC1 6 3 3 TOPIC2 4 1 3 TOPIC3 9 5 4 . . . . . . . . . . . . Result should look like TOPIC1 11 4 7 TOPIC2 7 3 4 (1 Reply)
Discussion started by: oraclermanpt
1 Replies

4. Shell Programming and Scripting

awk to Sum columns when other column has duplicates and append one column value to another with Care

Hi Experts, Please bear with me, i need help I am learning AWk and stuck up in one issue. First point : I want to sum up column value for column 7, 9, 11,13 and column15 if rows in column 5 are duplicates.No action to be taken for rows where value in column 5 is unique. Second point : For... (1 Reply)
Discussion started by: as7951
1 Replies

5. UNIX for Advanced & Expert Users

Conversion of rows to columns using awk based om column value

HI, My Input file data is dn:adcfgeneral id:13343 Name:xxxxxx Password:iutyerwuitywue wpuwt tuiytruityrutyrwtyrwp dn:cdferwjyyyy id:3875 Name:yyyy Password :hgfdsjkfhdsfkdlshf dshfkldshfdklsfh interset:uiuiufj My output should be ... (6 Replies)
Discussion started by: dineshaila
6 Replies

6. Shell Programming and Scripting

Need help with awk statement to break nth column in csv file into 3 separate columns

Hello Members, I have a csv file in the format below. Need help with awk statement to break nth column into 3 separate columns and export the changes to new file. input file --> file.csv cat file.csv|less "product/fruit/mango","location/asia/india","type/alphonso" need output in... (2 Replies)
Discussion started by: awk-admirer
2 Replies

7. Shell Programming and Scripting

Please Help!!!! Awk for summing columns based on selected column value

a,b,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,bb,cc,dd,ee,ff,gg,hh,ii a thru ii are digits and strings.... The awk needed....if coloumn 9 == i (coloumn 9 is string ), output the sum of x's(coloumn 22 ) in all records and sum of y's (coloumn 23 ) in all records in a file (records.txt).... (6 Replies)
Discussion started by: BrownBob
6 Replies

8. Shell Programming and Scripting

help sum columns by break in first column with awk or sed or something.

I have some data that is something like this? item: onhand counted location ITEM0001 1 0 a1 ITEM0001 0 1 a2 ITEM0002 5 0 b5 ITEM0002 0 6 c1 I want to sum up... (6 Replies)
Discussion started by: syadnom
6 Replies

9. Shell Programming and Scripting

Single column to multiple columns in awk

Hi - I'm new to the awk programming language. I'm trying to print a single column of data to several columns, and I found an article on iTWorld.com (ITworld.com - Printing in columns). It looks like the mkCols2 script is very close to what I need to do, but it looks like the end of the code... (2 Replies)
Discussion started by: astroDave
2 Replies

10. Shell Programming and Scripting

How to check Null values in a file column by column if columns are Not NULLs

Hi All, I have a table with 10 columns. Some columns(2nd,4th,5th,7th,8th and 10th) are Not Null columns. I'll get a tab-delimited file and want to check col by col and generate seperate error code for each col eg:102 if 2nd col value is NULL and 104 if 4th col value is NULL so on... I am a... (7 Replies)
Discussion started by: Mandab
7 Replies
Login or Register to Ask a Question