Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-03-2010
Registered User
 

Join Date: Oct 2008
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
remove column and store output to a variable

Hello guys

I need to run a script to remove the last column of different comma separated files.
The problem is that the number of columns of my files will be different and I won't know that number every time i run my script.

Is there any command I can use to remove the last column without specifying its number?
I can use cut but only when I know the column position.

I was trying another approach. If I calculate the number of the last column then I could use cut.
To find the last column I use:

Code:
cat filename | awk 'BEGIN {FS=","} END {print NF}'

but now I have the problem of how do I store that value in a variable so I can use cut?
If I use:

Code:
position='cat filename | awk 'BEGIN {FS=","} END {print NF}' '

I get this error: bash: FS=,: command not found

Thanks in advance

loperam

Last edited by Scott; 09-03-2010 at 02:36 PM.. Reason: Please use code tags
Sponsored Links
    #2  
Old 09-03-2010
bartus11's Avatar
Registered User
 

Join Date: Apr 2009
Posts: 2,654
Thanks: 3
Thanked 789 Times in 777 Posts
You can remove last column using just AWK:
Code:
awk -F\, -vOFS=\, '{NF=(NF>0)?NF-1:0}1' file


Last edited by bartus11; 09-03-2010 at 02:41 PM.. Reason: fixed for empty lines in a file
Sponsored Links
    #3  
Old 09-03-2010
Scott's Avatar
Scott Scott is offline Forum Staff  
Administrator
 

Join Date: Jun 2009
Location: Switzerland - ZH
Posts: 5,352
Thanks: 130
Thanked 535 Times in 473 Posts
Hi.

Using NF will print the field number, not the value of the field. For the value, use $NF.

If I understand you correctly, you want to store all the last comma-separated fields in one variable?


Code:
$ position=$(sed "s/.*,//" file1)

(no need to cat)

Last edited by Scott; 09-03-2010 at 02:52 PM.. Reason: Removed output. It's from an input file not related to the question
    #4  
Old 09-03-2010
Registered User
 

Join Date: Oct 2008
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for replying guys

bartus11
When using awk -F\, -vOFS=\, '{$NF=$NF-1}1' file
I get my file but the last column shows the value -1
What I want is that last column to be removed, not to show any other value

Scottn
I don't need to store all the last comma-separated files in one variable.
What I was trying to do as a second approach was to identify the position of that last column and store that single position number in a variable so I could use cut to remove it
What else can I use to get that position number?

Thanks
Sponsored Links
    #5  
Old 09-03-2010
bartus11's Avatar
Registered User
 

Join Date: Apr 2009
Posts: 2,654
Thanks: 3
Thanked 789 Times in 777 Posts
Check my code. It is NF not $NF. Also use my updated version, so you don't have problems when file contains empty lines.
Sponsored Links
    #6  
Old 09-03-2010
Scott's Avatar
Scott Scott is offline Forum Staff  
Administrator
 

Join Date: Jun 2009
Location: Switzerland - ZH
Posts: 5,352
Thanks: 130
Thanked 535 Times in 473 Posts
I apologise. I misread your question
Sponsored Links
    #7  
Old 09-03-2010
Registered User
 

Join Date: Oct 2008
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
that works as I needed!
thanks bartus11
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
store the output of "find" command in a variable? punitpa Shell Programming and Scripting 4 08-04-2009 07:21 AM
ksh: How to store each output line into a different variable? ksheller Shell Programming and Scripting 4 11-06-2008 07:01 AM
How to store the sql query's output in a variable venkatesh_sasi Shell Programming and Scripting 4 01-18-2008 12:03 AM
To store the output in a variable Sudhakar333 Shell Programming and Scripting 2 07-10-2007 08:45 AM
How to store output in variable when put in background sanjay92 UNIX for Dummies Questions & Answers 1 02-22-2005 02:41 PM



All times are GMT -4. The time now is 04:07 AM.