How to suppress error in following command?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to suppress error in following command?
# 1  
Old 09-29-2013
How to suppress error in following command?

I have a file containing data in multiple columns. The colums are seperated by pipe (|). I need to extract information as below:


Code:
myfile_20130929_781;10;100.00

where myfile.txt is the file name. 10 is the number of records in the file starting with 120 and 100.00 is the sum of 26th field of all lines starting with 120 in the file.

Code:
echo "myfile_20130929_781.txt"";""`grep ^120 myfile_20130929_781.txt | wc -l`"";"`grep ^120 myfile_20130929_781.txt |cut -f26 -d "|" | awk '{ sum+=$1} END {printf ("%0.2f\n", sum/100)}'`

if the file is moved during execution of above script, I am getting following error:


Code:
grep: can't open myfile_20130929_781.txt
grep: can't open myfile_20130929_781.txt
myfile_20130929_781.txt;0;0.00


How do I avoid first two lines and only get the third line as my output?

Thanks
Angshuman

Last edited by Don Cragun; 09-29-2013 at 02:38 PM.. Reason: Add CODE tags.
# 2  
Old 09-29-2013
Please use code tags as required by forum rules!

Not sure why you are using all those back ticks in your code snippet; use one single awk cmd:
Code:
awk '/^120/ {REC++; SUM+=$26; FN=FILENAME} END {print FN";"REC";"SUM}' myfile_20130929_781.txt

You may need to adapt your input and output field separators
# 3  
Old 09-29-2013
Hi RudiC,

Thank you for your reply and sincere apology for the mistake.

I am not that good in awk. Can you please help me to understand the command that you have suggested?

Thanks
Angshuman
# 4  
Old 09-29-2013
Code:
awk     '/^120/         {REC++; SUM+=$26; FN=FILENAME}  # if a line starts with 120, increment RECords count, SUM field 26, keep filename
         END            {print FN";"REC";"SUM}          # when file done, print above captured variables
        ' myfile_20130929_781.txt                       # file to work on

This User Gave Thanks to RudiC For This Post:
# 5  
Old 09-29-2013
Hi RudiC,

Thanks for the detailed explanation. The sum of the 26th column is too big hence I get the output in scientific notation. I think I need to use printf function here but not sure how to use the same here.

Your help is highly appreciated.

Thanks
Angshuman
# 6  
Old 09-30-2013
Using array .. incase providing multiple file.
Code:
awk '$0 ~ /^120/ { a[FILENAME]++;sum[FILENAME]+=$26;} END { for (i in a) { print i";"a[i]";"sum[i];}}' file1 file2

# 7  
Old 09-30-2013
This should avoid scientifc notation in awk
Code:
{printf "%s;%d;%.f\n",FN,REC,SUM}


Last edited by MadeInGermany; 09-30-2013 at 10:03 AM.. Reason: printf not print
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Tcsh: How to suppress error messages.

Hallo, I wrote some script: 95% of the script's output consists of error messages like "mkdir: cannot create directory ‘final': File exists Exit 1" and "rm: No match. Exit 1". These messages are not harmful at all, but they make the output almost unreadable. How can I get rid of... (5 Replies)
Discussion started by: DanielDD
5 Replies

2. Shell Programming and Scripting

Suppress Error Message

How can I suppress a error message being given by awk command in bash shell? (2 Replies)
Discussion started by: Prachi Gupta
2 Replies

3. Shell Programming and Scripting

How to suppress the error while copying the file

HI , I am tryin to copying multiple files from some dir. If the files are not present. It should not throw error in the screen. HOw to do that . Please help (4 Replies)
Discussion started by: arukuku
4 Replies

4. HP-UX

glance adviser suppress newline in print command

Hi, I have a glance adviser, the highlights below. The problem that i have is that every time glance finds process name "abc" it write the memory region data in a new line. My question is if i have a way to print without newline? The output line for process abc looks something like this:... (0 Replies)
Discussion started by: yochaia01
0 Replies

5. Shell Programming and Scripting

how to suppress dd output?

I have to stop the output of dd from writing to terminal. Here is the command: sudo dd if=boot1h of="/dev/r$temp1" Here is the output: 2+0 records in 2+0 records out 1024 bytes transferred in 0.000804 secs (1273715 bytes/sec) I have tried >> log.txt but it doesn't work. Is there... (4 Replies)
Discussion started by: msf5042
4 Replies

6. Shell Programming and Scripting

how to suppress list number from history command output

i run history command and I want to eliminate the list number. So far this perl script works as long as the list is a exact 3 character long. cat dd | perl -pe 's,\d{3},,' 70 export JAVA_HOME=. 81 export JAVA_HOME=. 82 export JAVA_HOME=`pwd` export JAVA_HOME=`pwd` ... (1 Reply)
Discussion started by: soemac
1 Replies

7. Shell Programming and Scripting

Suppress error message in shell script

Hi All this is a simple script #! /bin/bash FileCnt=`ls -lrt $DIR/* | wc -l` echo $FileCnt how could i escape the error msg if there are no files in $DIR ls: /home/sayantan/test/files/cnt/*: No such file or directory 0 Looking forward for a quick reply Regards, Newbie... (3 Replies)
Discussion started by: newbie07
3 Replies

8. Shell Programming and Scripting

Suppress error message in unzip

I'm creating a bsh shell to unzip a file from one directory into another. The directory that holds the zip files has zip files constantly being added to it, so I am testing it before it does the unzip and more. Right now my code looks like this: unzip -tq $ZIP_PATH/$ZIP_NAME >/dev/null if ... (5 Replies)
Discussion started by: skwyer
5 Replies

9. Shell Programming and Scripting

How to suppress error messages in script

I am getting the following upon cat a file which is not present in directory. "cat: cannot open test1.txt" I need to process files and I want that this message should be suppressed. thx (5 Replies)
Discussion started by: helper2007
5 Replies
Login or Register to Ask a Question