Use variables for cut command


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Use variables for cut command
# 1  
Old 02-10-2014
Use variables for cut command

#!/bin/sh

I have a file matrix.pair.cols that contains numbers like shown below:

Code:
case1	17	18
case2	1	2
case3	4	5
case4 ..	..



I want to cut the column numbers mentioned in col2 and 3 from another file
I have tried the code below, but no avail.

Code:
#!/bin/sh
 
while read p q r
do
	cut -f "$q,$r" matrix > $p.cols
done < matrix.pair.cols

exit 0


Suggestions on what I need to do differently?

Thanks,
Guss
# 2  
Old 02-10-2014
Quote:
Originally Posted by Gussifinknottle
Code:
#!/bin/sh
 
while read p q r
do
	cut -f "$q,$r" matrix > $p.cols
done < matrix.pair.cols

exit 0

I suggest you first make sure your variables are set correctly. Try:

Code:
#!/bin/sh
 
while read p q r ; do
	echo p: \"$p\"    q: \"$q\"    r: \"$r\"
	echo "cut -f \"$q,$r\" matrix > $p.cols"
done < matrix.pair.cols

exit 0

It might be that your source file contains hidden formatting characters which will not show in the copy here but may interrupt the reading of your file. It never hurts to ascertain each step separately.

The next thing is to apply the printed cut-commands manually one by one and see if they work as expected. One problem might be that you need to specify a field-delimiter (see option "-d" of "cut") to parse the file "matrix" correctly (not all cut-versions will understand tabs instead of spaces, for instance).

Btw: in scripts i would - just to be 1000% sure - always write "${var}" instead of "$var". In most cases the latter works the same, but in some combinations (within complex statements) the former might avoid ambiguity. Therefore:

Code:
	echo "cut -f \"${q},${r}\" matrix > ${p}.cols"

I hope this helps.

bakunin
# 3  
Old 02-10-2014
What exactly is going wrong?
# 4  
Old 02-10-2014
Is the field delimiter in the file matrix a tab character? If not, you'll need to add a -d option to your cut command or convert your field numbers to character positions and use something other than the -f option.
# 5  
Old 02-10-2014
Hello

#Don:
Code:
matrix

is a tab delimited file. I have used the
Code:
-d

option successfully for other delimiters
Code:
(space, ',').

#RudiC: I am just not getting an output as I ran it.

#bakunin: I tried running your code as well; no output here either.

Thank you for all your inputs!

~Guss
# 6  
Old 02-10-2014
Quote:
Originally Posted by Gussifinknottle
#RudiC: I am just not getting an output as I ran it.
There is no output to expect. The script as you showed it writes a lot of output files and you will have to examine these for any output.

Quote:
Originally Posted by Gussifinknottle
#bakunin: I tried running your code as well; no output here either.
If there is no output (that is: none of the files to be expected) then your input file (matrix.pair.cols) isn't even read. Make sure the file is accessible, in the directory you expect it to be (in your script this is the current directory, regardless of where the script may be). Modify your script further to test this:

Code:
#!/bin/sh

input="matrix.pair.cols"

if [ ! -r $input ] ; then
     echo "ERROR: file $input not readable or not existing at all"
     exit 2
fi

while read p q r ; do
	echo p: \"$p\"    q: \"$q\"    r: \"$r\"
	echo "cut -f \"$q,$r\" matrix > $p.cols"
done < matrix.pair.cols

exit 0

I hope this helps.

bakunin
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Cut command: can't make it cut fields

I'm a complete beginner in UNIX (and not a computer science student either), just undergoing a tutoring course. Trying to replicate the instructions on my own I directed output of the ls listing command (lists all files of my home directory ) to My_dir.tsv file (see the screenshot) to make use of... (9 Replies)
Discussion started by: scrutinizerix
9 Replies

2. UNIX for Dummies Questions & Answers

Cut pid from ps using cut command

hay i am trying to get JUST the PID from the ps command. my command line is: ps -ef | grep "mintty" | cut -d' ' -f2 but i get an empty line. i assume that the delimiter is not just one space character, but can't figure out what should i do in order to do that. i know i can use awk or cut... (8 Replies)
Discussion started by: ran ber
8 Replies

3. Shell Programming and Scripting

Cut fields from /etc/passwd file into variables?

The script must ask the user to enter the user name and check whether the user exists in /etc/passwd (you must allow the partial usernames also). If the username exists, display the details as: List of users Login Name: User ID: ... (3 Replies)
Discussion started by: nobletechnology
3 Replies

4. Shell Programming and Scripting

Cut Command error cut: Bad range

Hi Can anyone what I am doing wrong while using cut command. for f in *.log do logfilename=$f Log "Log file Name: $logfilename" logfile1=`basename $logfilename .log` flength=${#logfile1} Log "file length $flength" from_length=$(($flength - 15)) Log "from... (2 Replies)
Discussion started by: dgmm
2 Replies

5. UNIX for Dummies Questions & Answers

Parse or cut concat variables to individual values

Hello I need to pass some environment parameters to a datastage job and am getting an error when trying to send the complete concatinated variable. I have decided to parse out just the values and send as parameters but am struggling to find the best way to do this (actually I am not very... (3 Replies)
Discussion started by: LynnC
3 Replies

6. Shell Programming and Scripting

Need help for Cut Command

Hi All, Greetings.. I am having a Line of 1600 characters in which each specifi fields have some values. For example 1-5 Firstname 6-8 Age and so on.. I am using `expr substr $line 100,7` to get values from the line and store in seperate variables.. The file contains 70000 lines. It is taking 3... (8 Replies)
Discussion started by: dinesh1985
8 Replies

7. Shell Programming and Scripting

assign a command line argument and a unix command to awk variables

Hi , I have a piece of code ...wherein I need to assign the following ... 1) A command line argument to a variable e.g origCount=ARGV 2) A unix command to a variable e.g result=`wc -l testFile.txt` in my awk shell script When I do this : print "origCount" origCount --> I get the... (0 Replies)
Discussion started by: sweta_doshi
0 Replies

8. UNIX for Dummies Questions & Answers

Cut a Variable into sub variables based on a delimiter

Hello All, I am novice on Shell Scripting. Any help on this is highly appreciated. I have a variable $VARIABLE="$some1|$some2|$some3" I need sub variables $SUBVAR1,$SUBVAR2,$SUBVAR3 which must be equal to $some1 , $some2 and $some3 respectively. It works fine with $SUBVAR1 =... (6 Replies)
Discussion started by: jingi1234
6 Replies

9. Shell Programming and Scripting

Assign variables with cut

I need to read a file (a list) and assign the value to a variable (for each line), I'm looping until the end of the file. My problem is, I want to assign 2 separate variables from the list. The process I'm using is: awk '{print $3}' file1 > file2 awk '{print $4}' file1 > file3 cat file2... (2 Replies)
Discussion started by: douknownam
2 Replies
Login or Register to Ask a Question