How to print the without last column?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to print the without last column?
# 8  
Old 10-25-2014
Code:
sed 's/|[^|]*$//' file

# 9  
Old 10-25-2014
If all you want to do is to get rid of the last field and none of your input lines contain a single field, MadeInGermany's suggestion using sed is probably better than using awk. If you want to do this using any awk (/usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk on Solaris/SunOS systems), try:
Code:
nawk '
BEGIN {	FS = OFS = "|" }
NF < 2 {print ""; next }
{	NF -= 1; $1 = $1; print }
' file


Last edited by Don Cragun; 10-26-2014 at 04:07 AM.. Reason: Fix typo (s/all/none/).
# 10  
Old 10-25-2014
Code:
perl -wlnaF\| -e '@F = splice @F, 0, -1; $" = "|"; print "@F"' file

Code:
perl -wlnaF\| -e '$" = "|"; print "@F[0..$#F-1]";' file


Last edited by Aia; 10-25-2014 at 08:49 PM..
# 11  
Old 10-26-2014
Empty the last field then remove its delimiter
Code:
sed 's/[^|]*$//;s/|$//' file

# 12  
Old 10-26-2014
The trouble with manipulation of NF is that its effect is not specified by POSIX, so if there is an effect, it is implementation-specific.
So I think in awk the regex route is the most reliable way, or removing the last field and field separator, so:
Code:
awk '{$NF=x; sub(/\|$/,x)}1' FS=\| OFS=\| file

(which is analogous to MadeinGermany's sed suggestion) or
Code:
awk '{sub(/\|?[^|]*$/,x)}1' file

With sed:
Code:
sed 's/|\{0,1\}[^|]*$//' file

GNU sed -r or BSD sed -E :
Code:
sed -r 's/\|?[^|]*$//' file

--
Quote:
Originally Posted by Don Cragun
If all you want to do is to get rid of the last field and none of your input lines contain a single field, MadeInGermany's suggestion using sed is probably better than using awk. If you want to do this using any awk (/usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk on Solaris/SunOS systems), try:
Code:
nawk '
BEGIN {	FS = OFS = "|" }
NF < 2 {print ""; next }
{	NF -= 1; $1 = $1; print }
' file

Hi Don, that works with nawk, but it does not appear to work with /usr/xpg4/bin/awk .
I have not come across /usr/xpg6/bin/awk is that part of Solaris 11 or is that available as part of an additional package?

Last edited by Scrutinizer; 10-26-2014 at 08:06 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 13  
Old 10-26-2014
Quote:
Originally Posted by Scrutinizer
The trouble with manipulation of NF is that its effect is not specified by POSIX, so if there is an effect, it is implementation-specific.
So I think in awk the regex route is the most reliable way, or removing the last field and field separator, so:
Code:
awk '{$NF=x; sub(/\|$/,x)}1' FS=\| OFS=\| file

(which is analogous to MadeinGermany's sed suggestion) or
Code:
awk '{sub(/\|?[^|]*$/,x)}1' file

With sed:
Code:
sed 's/|\{0,1\}[^|]*$//' file

GNU sed -r or BSD sed -E :
Code:
sed -r 's/\|?[^|]*$//' file

--

Hi Don, that works with nawk, but it does not appear to work with /usr/xpg4/bin/awk .
I have not come across /usr/xpg6/bin/awk is that part of Solaris 11 or is that available as part of an additional package?
Hi Scrutinizer,
Thanks for the information. I no longer have access to a Solaris system to test these types of things. I didn't realize there was a difference in the way NF was handled in nawk and /usr/xpg[46]/awk. (One key difference between the three is which shell is used when you use the awk system() function (/bin/sh, /usr/xpg4/bin/sh, or /usr/xpg6/bin/sh, respectively)). The Solaris nawk comes straight from the AT&T new awk from the 1980's. I don't remember where the base source for /usr/xpg[46]/bin/awk came from.

The /usr/xpg6 hierarchy should be available on any Solaris system released since about 2004 or 2005, but it may have been an install time option rather than part of the default install.

The code I suggested works on OS X, so it probably works on BSD derived versions of awk. (And, this isn't surprising since the new awk came out of AT&T's Research UNIX group rather than AT&T's Programmer's Workbench UNIX group.)
# 14  
Old 10-27-2014
Code:
nawk -F"|"  'NR>0{NF-=1}$1=$1' OFS="|" file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need awk or Shell script to compare Column-1 of two different CSV files and print if column-1 matche

Example: I have files in below format file 1: zxc,133,joe@example.com cst,222,xyz@example1.com File 2 Contains: hxd hcd jws zxc cst File 1 has 50000 lines and file 2 has around 30000 lines : Expected Output has to be : hxd hcd jws (5 Replies)
Discussion started by: TestPractice
5 Replies

2. Shell Programming and Scripting

Help with subtraction column by column and repeat print out with N

Input file: 2 10 15 20 24 Output file 2 8 NNNNNNNN 10 5 NNNNN 15 5 NNNNN 20 4 NNNN 24 Do anybody experience subtraction column by column and print out number of subtraction times with N? The second column of the output file is the subtraction of 10-2 = 8; The third column just... (3 Replies)
Discussion started by: perl_beginner
3 Replies

3. Shell Programming and Scripting

Help with compare two column and print out column with smallest number

Input file : 5 20 500 2 20 41 41 0 23 1 Desired output : 5 2 20 0 1 By comparing column 1 and 2 in each line, I hope can print out the column with smallest number. I did try the following code, but it don't look good :( (2 Replies)
Discussion started by: perl_beginner
2 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. Shell Programming and Scripting

Print every 5 4th column values as separate row with different first column

Hi, I have the following file, chr1 100 200 20 chr1 201 300 22 chr1 220 345 23 chr1 230 456 33.5 chr1 243 567 90 chr1 345 600 20 chr1 430 619 21.78 chr1 870 910 112.3 chr1 914 920 12 chr1 930 999 13 My output would be peak1 20 22 23 33.5 90 peak2 20 21.78 112.3 12 13 Here the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

6. Shell Programming and Scripting

awk command to print only selected rows in a particular column specified by column name

Dear All, I have a data file input.csv like below. (Only five column shown here for example.) Data1,StepNo,Data2,Data3,Data4 2,1,3,4,5 3,1,5,6,7 3,2,4,5,6 5,3,5,5,6 From this I want the below output Data1,StepNo,Data2,Data3,Data4 2,1,3,4,5 3,1,5,6,7 where the second column... (4 Replies)
Discussion started by: ks_reddy
4 Replies

7. Shell Programming and Scripting

comparing column of two different files and print the column from in order of 2nd file

Hi friends, My file is like: Second file is : I need to print the rows present in file one, but in order present in second file....I used while read gh;do awk ' $1=="' $gh'" {print >> FILENAME"output"} ' cat listoffirstfile done < secondfile but the output I am... (14 Replies)
Discussion started by: CAch
14 Replies

8. Shell Programming and Scripting

print first few lines, then apply regex on a specific column to print results.

abc.dat tty cpu tin tout us sy wt id 0 0 7 3 19 71 extended device statistics r/s w/s kr/s kw/s wait actv wsvc_t asvc_t %w %b device 0.0 133.2 0.0 682.9 0.0 1.0 0.0 7.2 0 79 c1t0d0 0.2 180.4 0.1 5471.2 3.0 2.8 16.4 15.6 15 52 aaaaaa1-xx I want to skip first 5 line... (4 Replies)
Discussion started by: kchinnam
4 Replies

9. Shell Programming and Scripting

find expression with awk in only one column, and if it fits, print whole column

Hi. How do I find an expression with awk in only one column, and if it fits, then print that whole column. 1 apple oranges 2 bannanas pears 3 cats dogs 4 hesaid shesaid echo "which number:" read NUMBER (user inputs number 2 for this example) awk " /$NUMBER/ {field to search is field... (2 Replies)
Discussion started by: glev2005
2 Replies

10. Shell Programming and Scripting

Question about sort specific column and print other column at the same time !

Hi, This is my input file: ali 5 usa abc abu 4 uk bca alan 6 brazil bac pinky 10 utah sdc My desired output: pinky 10 utah sdc alan 6 brazil bac ali 5 usa abc abu 4 uk bca Based on the column two, I want to do the descending order and print out other related column at the... (3 Replies)
Discussion started by: patrick87
3 Replies
Login or Register to Ask a Question