extract column with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extract column with awk
# 1  
Old 09-24-2012
extract column with awk

This is a example TXT file:
HTML Code:
ID CDR1 CDR2 CDR3
1 CDR1   CDR3
2 CDR1 CDR2 CDR3
3     CDR3   
What I want to do is extract the fourth column with awk, however, there are some blank space in some field. I get wrong result when it print out awk result.

Code:
awk '{print $1,$4}' awk_column_test.txt

How will i deal with these blank spaces?

If I want print 'NULL' when the field is blank, how will I should do?

Thank you very much!
# 2  
Old 09-24-2012
Code:
cat file

ID CDR1 CDR2 CDR3
1 CDR1   CDR3
2 CDR1 CDR2 CDR3
3     CDR3

awk '$4 == ""{$4="NULL"}{print $4}' file

CDR3
NULL
CDR3
NULL

This User Gave Thanks to in2nix4life For This Post:
# 3  
Old 09-24-2012
What separator is used in your text file? Tab, space?

You might be able to tell it to use a single space or tab as the separator rather than globbing all whitespace...

Code:
awk -F"\t" '{ print $1,$4 }'
awk -F" " '{ print $1,$4 }'

This User Gave Thanks to Corona688 For This Post:
# 4  
Old 09-24-2012
Try this
Code:
~/unix.com$ awk -F\| 'gsub(/ /,"|")&&$4=$4?$4:"NULL"{print $1,$4}' f

The above script is overcomplicated, use Corona688's or in2nix4life's suggestion instead Smilie

Last edited by tukuyomi; 09-24-2012 at 01:56 PM..
This User Gave Thanks to tukuyomi For This Post:
# 5  
Old 09-24-2012
Sorry, I can't express clearly what I mean.

The txt file could be understand like this:

ID CDR1 CDR2 CDR3
1 CDR1 NULL CDR3
2 CDR1 CDR2 CDR3
3 NULL NULL CDR3

The results I want to get should be four "CDR3". "NULL" here represents blank space, so maybe I should deal with the original file?

Thanks a lot!

Quote:
Originally Posted by in2nix4life
Code:
cat file

ID CDR1 CDR2 CDR3
1 CDR1   CDR3
2 CDR1 CDR2 CDR3
3     CDR3

awk '$4 == ""{$4="NULL"}{print $4}' file

CDR3
NULL
CDR3
NULL

---------- Post updated at 12:08 PM ---------- Previous update was at 11:50 AM ----------

Sorry, I can't express clearly what I mean. I use space as the separator.

The txt file could be understand like this:

ID CDR1 CDR2 CDR3
1 CDR1 NULL CDR3
2 CDR1 CDR2 CDR3
3 NULL NULL CDR3

The results I want to get should be four "CDR3". "NULL" here represents blank space, so maybe I should deal with the original file?

Thanks a lot!

Quote:
Originally Posted by Corona688
What separator is used in your text file? Tab, space?

You might be able to tell it to use a single space or tab as the separator rather than globbing all whitespace...

Code:
awk -F"\t" '{ print $1,$4 }'
awk -F" " '{ print $1,$4 }'

# 6  
Old 09-24-2012
Try -F"[ ]" if you're on Linux
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 09-24-2012
From your sample code, it's impossible to find CDR3 on second and fourth line (ID 1 & 3). Is there any [tab] and [spaces] mixed?

Last edited by tukuyomi; 09-24-2012 at 03:10 PM..
This User Gave Thanks to tukuyomi 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

awk script to extract a column, replace one of the header and replace year(from ddmmyy to yyyy)

I have a csv which has lot of columns . I was looking for an awk script which would extract a column twice. for the first occurance the header and data needs to be intact but for the second occurance i want to replace the header name since it a duplicate and extract year value which is in ddmmyy... (10 Replies)
Discussion started by: Kunalcurious
10 Replies

2. Shell Programming and Scripting

Get extract and replace column with link in a column where it exists

hi i have sample data a,b,c,d,e,g h http://mysite.xyx z,b,d,f,e,s t http://123124# a,b,c,i,m,nothing d,i,j,e,w,nothing output expected is a,b,c,d,e,http://mysite.xyx z,b,d,f,e,http://123124# a,b,c,i,m,nothing d,i,j,e,w,nothing i can get only links using grep -o 'http.*' i... (8 Replies)
Discussion started by: zozoo
8 Replies

3. Shell Programming and Scripting

awk to extract value from column using variable

I am having trouble extracting the value in the columns declared in a variable. I have tried several different variation of awk but both give me the column number and not the actual value of that column. Any suggestions? Neither of the "extract" variables below are performing as desired ... (5 Replies)
Discussion started by: ncwxpanther
5 Replies

4. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

5. UNIX for Dummies Questions & Answers

awk - Extract 4 lines in Column to Rows Tab Delimited between tags

I have tried the following to no avail. xargs -n8 < test.txt awk '{if(NR%6!=0){p=""}else{p="\n"};printf $0" "p}' Mod_Alm_log.txt > test.txt I have tried different variations of the above, the problem is mixes lines together. And it includes the tags "%a and %A" I need them to be all tab... (16 Replies)
Discussion started by: mytouchsr
16 Replies

6. Shell Programming and Scripting

Compare files & extract column awk

I have two tab delimited files as given below: File_1: PV16 E1 865 2814 1950 PV16 E2 2756 3853 1098 PV16 E4 3333 3620 288 PV16 E5 3850 4101 252 PV16 E6 83 559 477 PV16 E7 562 858 297 PV16 L2 4237 5658 ... (10 Replies)
Discussion started by: vaibhavvsk
10 Replies

7. Shell Programming and Scripting

Need to extract data from Column having variable length column

Hi , I need to extract data from below mentioned data, having no delimiter and havin no fixed column length. For example: Member nbr Ref no date 10000 1000 10202012 200000 2000 11202012 Output: to update DB with memeber nbr on basis of ref no. ... (6 Replies)
Discussion started by: ns64110
6 Replies

8. Shell Programming and Scripting

for each different entry in column 1 extract maximum values from column 2 in unix/awk

Hello, I have 2 columns (1st column has multiple entries but the corresponding values in the column 2 may be the same or different.) however I want to extract unique values for each entry in column 1 by assigning the max value from column 2 SDF4 -0.211654 SDF4 0.978068 ... (1 Reply)
Discussion started by: Diya123
1 Replies

9. Shell Programming and Scripting

awk/sed to extract column bases on partial match

Hi I have a log file which has outputs like the one below conn=24,196 op=1 RESULT err=0 tag=0 nentries=9 etime=3,712 dbtime=0 mem=486,183,328/2,147,483,648 Now most of the time I am only interested in the time ( the first column) and a column that begins with etime i.e... (8 Replies)
Discussion started by: pkabali
8 Replies

10. Shell Programming and Scripting

How to extract a column from two different files in AWK?

Hi guys, I need help in extracting one column of numbers from two different files and display it in a output file. In specific, I want to extrac the column no.2 ($2) from each file, file1.txt, file2.txt. Then place both extracted columns in a one file, out.txt. the line command I use to... (7 Replies)
Discussion started by: solracq
7 Replies
Login or Register to Ask a Question