Visit Our UNIX and Linux User Community


assigning complicated input to a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting assigning complicated input to a variable
# 1  
Old 08-29-2009
assigning complicated input to a variable

I cannot seem to correctly assign this line of code to a variable:

HITS=`pwd`/csv/LightningProbability30minAlignmentErrorTable_*.csv | sed 's/,/ /g' | awk '{print int(100*$5)}' | wc

This does not work.

I tried surrounding it in backticks, but I think the backticks in `pwd` is messing it up.

Any ideas?
# 2  
Old 08-29-2009
You do not need the pwd so try just pointing at the directory under the current directory using a relative path instead of an absolute path like this:
Code:
HITS=`csv/LightningProbability30minAlignmentErrorTable_*.csv | sed 's/,/ /g' | awk '{print int(100*$5)}' | wc`

# 3  
Old 08-30-2009
Quote:
Originally Posted by sigtor77
I cannot seem to correctly assign this line of code to a variable:

HITS=`pwd`/csv/LightningProbability30minAlignmentErrorTable_*.csv | sed 's/,/ /g' | awk '{print int(100*$5)}' | wc

This does not work.

What does "does not work." mean? What does happen? What error messages do you get? What did you expect to happen?
Quote:
I tried surrounding it in backticks, but I think the backticks in `pwd` is messing it up.

What is "it"?

Did you have nested backticks? If so, did you escape the inner ones?
Quote:

Any ideas?
# 4  
Old 08-30-2009
Hi.

Quote:
Originally Posted by TonyFullerMalv
You do not need the pwd so try just pointing at the directory under the current directory using a relative path instead of an absolute path like this:
Code:
HITS=`csv/LightningProbability30minAlignmentErrorTable_*.csv | sed 's/,/ /g' | awk '{print int(100*$5)}' | wc`

Here you're trying to execute all the CSV files. I'd guess this won't work.


Quote:
Originally Posted by sigtor77
I cannot seem to correctly assign this line of code to a variable:

HITS=`pwd`/csv/LightningProbability30minAlignmentErrorTable_*.csv | sed 's/,/ /g' | awk '{print int(100*$5)}' | wc

This does not work.

I tried surrounding it in backticks, but I think the backticks in `pwd` is messing it up.

Any ideas?
It's hard to say, because what you're trying to do isn't totally clear.

You want to assign a path to a variable, remove commas from the path, pass that to awk (without actually echo'ing it) and multiply the fifth field by 100, then get a word count.

Sounds like you wanted to cat the files (otherwise what use is sed in this?). And why go to the trouble of multiplying the fifth field and then pipiing the output through wc?

Perhaps something like:

Code:
HITS=$(awk -F, '{print int(100*$5)}' csv/LightningProbability30minAlignmentErrorTable_*.csv )

If you want to use wc, then the multiplication inside awk serves no purpose because wc will only show the lines, words and characters:
Code:
HITS=$(cat csv/LightningProbability30minAlignmentErrorTable_*.csv | sed "s/,/ /g" | wc)

(if your fields in the CSV files have spaces in them, then the wc output is meaningless)


If you want a record count from each file then use
Code:
HITS=$(wc csv/LightningProbability30minAlignmentErrorTable_*.csv)


Last edited by Scott; 08-30-2009 at 06:37 AM..
# 5  
Old 08-30-2009
Computer

yes, I should be more specific...

cat `pwd`/csv/LightningProbability30minAlignmentErrorTable_*.csv | sed 's/,/ /g' | awk '{print int(100*$5)}' | grep "^0" | wc -l

this prints out the number of zeroes in the 5th 'column' of all of the csv files in the directory. So I am simply trying to assign it to a variable.

HITS=cat `pwd`/csv/LightningProbability30minAlignmentErrorTable_*.csv | sed 's/,/ /g' | awk '{print int(100*$5)}' | grep "^0" | wc -l

this does not work, and the error message was along the lines of 'file or directory does not exist'. When I surrounded the RHS of the expression with backticks, I had the same result.

However, this worked as desired:
HITS=`cat /csv/LightningProbability30minAlignmentErrorTable_*.csv | sed 's/,/ /g' | awk '{print int(100*$5)}' | grep "^0" | wc -l`

For some reason, I didn't think I still needed the cat in there.

Thanks for the help, all!
# 6  
Old 08-30-2009
Quote:
Originally Posted by sigtor77
However, this worked as desired:
HITS=`cat /csv/LightningProbability30minAlignmentErrorTable_*.csv | sed 's/,/ /g' | awk '{print int(100*$5)}' | grep "^0" | wc -l`

For some reason, I didn't think I still needed the cat in there.

You don't need cat:

Code:
HIST=$( sed 's/,/ /g' /csv/LightningProbability30minAlignmentErrorTable_*.csv | awk '{print int(100*$5)}' | grep "^0" | wc -l )

-- or sed or grep or wc:

Code:
HITS=$( awk -F, '$5 == 0 { ++x } END { print x }' /csv/LightningProbability30minAlignmentErrorTable_*.csv )

# 7  
Old 08-31-2009
That's the reason why I hate backticks. Hard to read, prone to errors due to keyboard mapping, display fonts etc.. A real pain (in the bash Smilie). For command substitution I prefer the $(...) construct as shown in scottand and cfajohnsonn's posts.

On top of that I thought they were obsolete...

Previous Thread | Next Thread
Test Your Knowledge in Computers #289
Difficulty: Easy
Sun Microsystems was acquired by Oracle Corporation in 2012.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Assigning a value to a variable

Hi I have a script that accepts an input date in YYYY-MM-DD format. After that, I used sed to delete the hyphen (-) which gives me an output YYYY MM DD. My question is, how can I assign those three numbers to a three different variable. Example: 2013-11-23 will become 2013 11 23... (4 Replies)
Discussion started by: erin00
4 Replies

2. Shell Programming and Scripting

problem in assigning value to variable have value fo other variable

my script is some thing like this i11="{1,2,3,4,5,6,7,8,9,10,11,}" echo "enter value" read value ..............suppose i11 x="$value" echo "$($value)" .............the echo should be {1,2,3,4,5,6,7,8,9,10,11,} but its showing "i11" only. plz help me out to get desired... (10 Replies)
Discussion started by: sagar_1986
10 Replies

3. Shell Programming and Scripting

sed with complicated variable pattern

Hi, Below is the content of the file how it looks: # EMAIL #export BMS_EMAIL_ENABLED=true export BMS_EMAIL_ENABLED=false #export BMS_EMAIL_SERVER=esasmtp01.kohls.com export BMS_EMAIL_SERVER=esasmtp01.kohls.com.SMTP_SERVICE export BMS_EMAIL_FROM_ADDRESS=ec_notify@kohlsectest.com export... (4 Replies)
Discussion started by: pravintse
4 Replies

4. Shell Programming and Scripting

Removing a character from a variable and assigning it to another variable?

Hi folks. I have this variable called FirstIN that contains something like this: 001,002,003,004... I am trying to assign the content of this variable into ModifiedIN but with the following format : 001 002 003 004...(changing the commas for spaces) I thought about using sed but i am not... (17 Replies)
Discussion started by: Stephan
17 Replies

5. Shell Programming and Scripting

Assigning values for a dynamic array for an input

Hello, Can somebody please give me a snippet for the below requirement. I want to assign the values separeted by a comma to be assigned to a dynamic array. If I give an input (read statement) like abc1,abc2,abc3,abc4,abc5, all these strings abc* should be assigned to an array like below... (2 Replies)
Discussion started by: suneelj
2 Replies

6. Shell Programming and Scripting

Assigning value to a variable

Is there any difference between: set variable=39 and variable=39 (1 Reply)
Discussion started by: proactiveaditya
1 Replies

7. Shell Programming and Scripting

Assigning value to a variable

can we make a global variable and store character values and add other values to that variable ?? for example a="hello, John" and can we add value ". How are you? so a can have "hello, John. How are you?" can someone help me?? (2 Replies)
Discussion started by: bonosungho
2 Replies

8. Shell Programming and Scripting

assigning a variable

hi all, in ksh, how do i assign the output of a find command to a variable, e.g am trying something like this : totalNoFiles=$(print find ./ -name "SystemOut*.log"); but when i echo $totalNoFiles it displays find ./ -name "SystemOut*.log" instead of the total number of... (2 Replies)
Discussion started by: cesarNZ
2 Replies

9. Shell Programming and Scripting

Assigning Value of variable

Hi In my shell script, I'm trying to find the line count of a file and assign it to a variable. LINE_COUNT=$(wc -l $FILE_NAME) But when i display LINE_COUNT, i'm getting the linecount concatenated with the file name. I want only the number. How can i get the line count alone ? Someone... (2 Replies)
Discussion started by: janemary.a
2 Replies

10. Shell Programming and Scripting

Assigning a value to variable

Another newbie to Unix scripting Q.. How do you assign a value resulting from a command, such as awk, to a variable. I am currently trying:- $awk '{print $1}' file1 > variable1 with no change to $variable1. The line: $awk '{print $1}' file1 does print the first line of the... (3 Replies)
Discussion started by: sirtrancealot
3 Replies

Featured Tech Videos