awk script to remove last two columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk script to remove last two columns
# 1  
Old 10-28-2014
awk script to remove last two columns

I am writing an awk script to remove the last two fields in a pipe delimited file. The script that I have currently deletes the last two fields but each field is being printed on its own line in the output. I can't seem to get the rows to print out on the same line as the input data. This is what I have so far.

Input file:

Code:
DATAA|2014|10|0|83|2|4|681|0|663|15|0|0|9|0|189|0|15|0|0|0|0|1661|1661
DATAB|2014|10|0|573|18|150|5739|213|4081|289|7|4|282|0|2929|146|2|0|0|0|12|14445|14445
DATAC|2014|10|0|7|0|0|224|0|400|4|0|0|0|0|43|19|0|0|0|0|0|697|697
DATAD|2014|10|0|0|0|0|4|0|0|0|0|0|0|0|0|0|0|0|0|0|0|4|4

My awk script to remove last two columns:
Code:
awk -F '|' '{for(i=1;i<=(NF-2);i++) print $i}'

Goal output:

Code:
DATAA|2014|10|0|83|2|4|681|0|663|15|0|0|9|0|189|0|15|0|0|0|0
DATAB|2014|10|0|573|18|150|5739|213|4081|289|7|4|282|0|2929|146|2|0|0|0|12
DATAC|2014|10|0|7|0|0|224|0|400|4|0|0|0|0|43|19|0|0|0|0|0
DATAD|2014|10|0|0|0|0|4|0|0|0|0|0|0|0|0|0|0|0|0|0|0

I have tried
Code:
awk -F '|' -v OFS="|" '{$(NF)="";$(NF-1)="";print $0}'

but do not like the empty brackets this creates. Any suggestions would greatly be appreciated!

Last edited by Don Cragun; 10-28-2014 at 12:31 AM.. Reason: Add CODE tags.
# 2  
Old 10-28-2014
Code:
awk 'NF{NF-=2}1' FS='|' OFS='|' infile

This User Gave Thanks to Akshay Hegde For This Post:
# 3  
Old 10-28-2014
Akshay Hegde you rock!
# 4  
Old 10-28-2014
What Akshay suggested will work with some versions of awk, but not all.

Your 1st attempt was close. The following should work with any standards-conforming awk:
Code:
awk -F'|' '{for(i = 1; i <= NF - 2; i++) printf("%s%s", $i, i == NF - 2 ? "\n" : OFS)}' OFS="|" file

On Solaris/SunOS systems, change awk to /usr/xp4/bin/awk, /usr/xpg6/bin/awk, or nawk.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 10-28-2014
You could use Perl as well, if you don't want to put up with all that "standards-conforming" baggage.
Code:
perl -wlnaF'\|' -e '$" = "|"; print "@F[0..$#F-2]"' file

This User Gave Thanks to Aia For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to loop my awk script across different columns?

input name a1 a2 a3 g1 0.8 0.4 0.2 g2 -0.2 -0.6 -0.7 g3 0.1 0.6 0.8 g4 0.1 0 0 g5 -0.2 -0.2 -0.2 g6 -0.1 -0.9 -0.9 g7 0 0 0.2 output a1:2 a2:0 a3:0 I use the following awk '{if ($2>=0.5) print $1"\t""1";... (3 Replies)
Discussion started by: quincyjones
3 Replies

2. Shell Programming and Scripting

awk script to split file into multiple files based on many columns

So I have a space delimited file that I'd like to split into multiple files based on multiple column values. This is what my data looks like 1bc9A02 1 10 1000 FTDLNLVQALRQFLWSFRLPGEAQKIDRMMEAFAQRYCQCNNGVFQSTDTCYVLSFAIIMLNTSLHNPNVKDKPTVERFIAMNRGINDGGDLPEELLRNLYESIKNEPFKIPELEHHHHHH 1ku1A02 1 10... (9 Replies)
Discussion started by: viored
9 Replies

3. Programming

awk processing / Shell Script Processing to remove columns text file

Hello, I extracted a list of files in a directory with the command ls . However this is not my computer, so the ls functionality has been revamped so that it gives the filesizes in front like this : This is the output of ls command : I stored the output in a file filelist 1.1M... (5 Replies)
Discussion started by: ajayram
5 Replies

4. Shell Programming and Scripting

How to remove spaces between the columns in UNIX script?.

Hi guru's, I am trying to write a script to generate a csv file by connecting to database run a query and put the values into csv file. But the problem i face is i am getting lot of space after one value.how can i remove those values?. Please help. #!/bin/bash export... (2 Replies)
Discussion started by: karingulanagara
2 Replies

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

6. Shell Programming and Scripting

Computing the ratio of similar columns in the two files using awk script

Thanks Bartus11 for your help in the following code to compare the two files "t1" and "t2". awk 'NR==FNR{a=1;next}$2 in a{print $2}' t1 t2 First can anyone explain that what is the purpose of assigning a =1? Second, the current script is printing out the matched columns between the... (4 Replies)
Discussion started by: coder83
4 Replies

7. Shell Programming and Scripting

Awk script to replace null columns with blank

I have a file with contents "08011"||20080812 "23348"|20080827|20080924 "23387"|20080829|20080915 "23581"|20081003|20081028 "23748"|20081017|20090114 "24095"|20080919|20081013 "24105"|20070723|20070801 "24118"|20080806|20081013 "24165"|20080820|20080912 "24221"|20080908|20080929 i... (3 Replies)
Discussion started by: sonam273
3 Replies

8. Shell Programming and Scripting

Single command for add 2 columns and remove 2 columns in unix/performance tuning

Hi all, I have created a script which adding two columns and removing two columns for all files. Filename: Cust_information_1200_201010.txt Source Data: "1","Cust information","123","106001","street","1-203 high street" "1","Cust information","124","105001","street","1-203 high street" ... (0 Replies)
Discussion started by: onesuri
0 Replies

9. Shell Programming and Scripting

AWK script to print all the columns excpet the one specified

I have several columns by the name A B C D E...... and I want to print all the column other than column C and D. Could you please help me with the awk script? Thanks!! (3 Replies)
Discussion started by: kn.naresh
3 Replies

10. Shell Programming and Scripting

Remove lines, Sorted with Time based columns using AWK & SORT

Hi having a file as follows MediaErr.log 84 Server1 Policy1 Schedule1 master1 05/08/2008 02:12:16 84 Server1 Policy1 Schedule1 master1 05/08/2008 02:22:47 84 Server1 Policy1 Schedule1 master1 05/08/2008 03:41:26 84 Server1 Policy1 ... (1 Reply)
Discussion started by: karthikn7974
1 Replies
Login or Register to Ask a Question