|
Search Forums:
|
|||||||
| Forums | Register | Forum Rules | Linux and Unix Links | Man Pages | Albums | FAQ | Users | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
You can remove last column using just AWK: Code:
awk -F\, -vOFS=\, '{NF=(NF>0)?NF-1:0}1' fileLast edited by bartus11; 09-03-2010 at 02:41 PM.. Reason: fixed for empty lines in a file |
| Sponsored Links | ||
|
|
|
#3
|
||||
|
||||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
||||
|
||||
|
I apologise. I misread your question
![]() |
| Sponsored Links | |
|
|
#7
|
|||
|
|||
|
that works as I needed!
thanks bartus11 |
| Sponsored Links | ||
|
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|