remove column and store output to a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting remove column and store output to a variable
# 1  
Old 09-03-2010
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 03:36 PM.. Reason: Please use code tags
# 2  
Old 09-03-2010
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 03:41 PM.. Reason: fixed for empty lines in a file
# 3  
Old 09-03-2010
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 03:52 PM.. Reason: Removed output. It's from an input file not related to the question
# 4  
Old 09-03-2010
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
# 5  
Old 09-03-2010
Check my code. It is NF not $NF. Also use my updated version, so you don't have problems when file contains empty lines.
# 6  
Old 09-03-2010
I apologise. I misread your question Smilie
# 7  
Old 09-03-2010
that works as I needed!
thanks bartus11
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Python - store output of command to a variable

I am trying to store output of python command in variable. Could you please help how I can do that ? For example I am executing the following command - "CentOS" in server_desc The output would be True or False I would like to store the output in a variable say outPut and use condition... (4 Replies)
Discussion started by: atanubanerji
4 Replies

2. Shell Programming and Scripting

Cut each column value and store in variable

Hi Pals, I have a file which contains a set of similar lines, as follows: Remedy Support Action Triggered by Incident Modification of type: ARA username2 ########## ARA|INC0000178532 INC0000178532 0000000019879 000000000038372 Remedy... (6 Replies)
Discussion started by: Khushbu
6 Replies

3. Shell Programming and Scripting

how to store output to a variable

I need some help: 1) I have a out put from a shell script, the out put looks like this: Attempting privilege escalation using sudo ... List backups for CLTST: Start date Status Ret. Class Label -------------------- ------------ ------------ ... (2 Replies)
Discussion started by: samk
2 Replies

4. Shell Programming and Scripting

Store a column from an excel sheet (exported to txt) into a variable

I typically pull a bunch of data via SQL that lists a bunch of users and the server on which they want to access, as well as other attributes, in one row of an excel sheet and the number of rows is directly proportionate to the number of users. I'm trying to write a loop to read each line of the... (2 Replies)
Discussion started by: MaindotC
2 Replies

5. Shell Programming and Scripting

store sqlplus output in variable

hi how can i store sqlplus output to a variable in sh script (not bash) Thanks MM (1 Reply)
Discussion started by: murtymvvs
1 Replies

6. Shell Programming and Scripting

date output store in variable problem

When I run following command date Output1 => Thu Sep 9 03:26:52 IST 2010 When I store in a varibale as a=`date` echo $a output2 => Thu Sep 9 03:27:02 IST 2010 The differnece is, it is trimming the space when I am storing the output in varibale. Output1 = Thu Sep 9 03:26:52 IST 2010... (2 Replies)
Discussion started by: pravincpatil
2 Replies

7. Shell Programming and Scripting

ksh: How to store each output line into a different variable?

Example output: /tmp/generatelines.sh line1 line2 line3 line4 I want each output line assigned to its own variable, ie: "line1" --> $a "line2" --> $b "line3" --> $c "line4" --> $d Is this possible without writing to a temporary file? Thanks (4 Replies)
Discussion started by: ksheller
4 Replies

8. Shell Programming and Scripting

How to store the sql query's output in a variable

Hi, My requirement is : We are calling an sql statement from a UNIX session, and fetching data into some variables from a table .. now we are unable to access these variables from outside the SQL part. Please let me know how can I achieve this. Can you please share a code snippet which... (4 Replies)
Discussion started by: venkatesh_sasi
4 Replies

9. Shell Programming and Scripting

To store the output in a variable

Hi, I am getting the following error while executing the script. Please can someone throw some light where is the problem. Many thanks. ./check: temp: not found The directory related to SEP instance 4 does not exist. The script is as follows. SEP_APP="/scp/sepx/app... (2 Replies)
Discussion started by: Sudhakar333
2 Replies

10. UNIX for Dummies Questions & Answers

How to store output in variable when put in background

Hi, How do I store following command output: export RESULT=`date` & It works when I do : export RESULT=`date` But what I need is when command put it background, I also need that output going to RESULT variable. Is there any way ? Thanks Sanjay (1 Reply)
Discussion started by: sanjay92
1 Replies
Login or Register to Ask a Question