Line/Variable Editing for Awk sed Cut


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Line/Variable Editing for Awk sed Cut
# 1  
Old 12-02-2010
Line/Variable Editing for Awk sed Cut

Hello,
i have a file, i open the file and read the line, i want to get the first item in the csv file and also teh third+6 item and wirte it to a new csv file. only problem is that using echo it takes TOO LONG:

please help a newbie. below is my code:
Code:
WorkingDir=$1
FileName=`cut -d ',' -f 2-2 $WorkingDir/info.csv | sed q`

while read line
do
        InstrName=`echo $line | cut -d ',' -f 1-1`
        VarC=`echo $line | cut -d ',' -f 3-3`
        VarD=`echo $line | cut -d ',' -f 4-4`
        VarF=`echo $line | cut -d ',' -f 6-6`

        if [ $VarC=$VarD ];
        then
                Total=$(($VarC + $VarF))
                echo $InstrName "," $Total >> $FileName
        else
                echo "ERROR IN SYSTEM!! SEND EMAIL"
        fi
done < $WorkingDir/solution.csv


Last edited by vbe; 12-02-2010 at 11:25 AM.. Reason: use code tags please
# 2  
Old 12-02-2010
Try this loop instead that reads the values directly into the variables:
Code:
while IFS=, read InstrName VarB VarC VarD VarE VarF x
do
  if [ "$VarC" = "$VarD" ]; then
    Total=$((VarC + VarF))
    echo "$InstrName , $Total"
  else
    echo "ERROR IN SYSTEM!! SEND EMAIL"
  fi
done < "$WorkingDir/solution.csv" > "$FileName"


Last edited by Scrutinizer; 12-02-2010 at 02:08 PM.. Reason: Added quotes around VarC and VarD in the test statement
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 12-02-2010
Excellent it worked in record time!! Smilie Thank you.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed and cut command in variable

hi, i want to remove 12 and 13 column from psv files and dump them in new folder ls -ltr *GTDA_Dly_Pmix_*.psv>filename.xls var1=`cat filename.xls` for i in $var1 do var3=`echo "$i" |cut -d '|' -f12,13 |sort -u` sed -e 's/"|$var3"//g... (2 Replies)
Discussion started by: renuk
2 Replies

2. Shell Programming and Scripting

sed or awk, cut, to extract specific data from line

Hi guys, I have been trying to do this, but... no luck so maybe you can help me. I have a line like this: Total Handled, Received, on queue Input Mgs: 140 / 14 => 0 I need to, get the number after the / until the =, to get only 14 . Any help is greatly appreciated. Thanks, (4 Replies)
Discussion started by: ocramas
4 Replies

3. Shell Programming and Scripting

editing file with awk cut and sed

HI All, I am new to unix. I have a file would like to do some editing by using awk, cut and sed. Could anyone help? This file contain 100 lines. There are one line for example: 2,"102343454",5060,"579668","579668","579668","SIP",,,"825922","035885221283026",1,268,"00:59:00.782 APR 17... (2 Replies)
Discussion started by: mimilaw
2 Replies

4. Shell Programming and Scripting

cut, sed, awk too slow to retrieve line - other options?

Hi, I have a script that, basically, has two input files of this type: file1 key1=value1_1_1 key2=value1_2_1 key4=value1_4_1 ... file2 key2=value2_2_1 key2=value2_2_2 key3=value2_3_1 key4=value2_4_1 ... My files are 10k lines big each (approx). The keys are strings that don't... (7 Replies)
Discussion started by: fzd
7 Replies

5. Shell Programming and Scripting

sed or awk editing help

Hi all I use aix (sadly). I've got a file consisting of fields separated by commas, I need a sed or awk command that will delete all spaces between two commas as long as there are only spaces between the commas. eg ,abc, ,sd , ,dr at would become ,abc,,sd ,,dr at I have... (53 Replies)
Discussion started by: mychmose
53 Replies

6. Homework & Coursework Questions

String editing using sed? awk?

1. The problem statement, all variables and given/known data: Problem Statement for project: When an account is created on the CS Unix network, a public html directory is created in the account's home directory. A default web page is put into that directory. Some users replace or... (13 Replies)
Discussion started by: peage1475
13 Replies

7. Shell Programming and Scripting

awk line editing

Hello I am trying to make an awk script that always replaces the same characters in a file, with white space in between. Can anyone write a script that prompts for the 3 numbers and then replaces them in the file? eg. enter a b c: 'it then puts a b c in the file. The file format is always ... (12 Replies)
Discussion started by: gav2251
12 Replies

8. Shell Programming and Scripting

Passing a variable to sed or cut

:confused: Is it possible to send a variable to a sed or cut command? I have a test script as below: counter=1 while read line do # Test the file printf "$line" > temp$counter pref=$(cut c1-2000 $temp$counter | sed 's///g' | sed 's|.*PutTime\(.*)Origin.*|\1|') printf" let... (5 Replies)
Discussion started by: gugs
5 Replies

9. Shell Programming and Scripting

SED - editing file names (End of line problem?)

For lists in sed, to say what to replace, is this correct: I am hoping that this would recognise that either a "." is present, or that the substitution happens at the end of the line. For files with extensions , my script works perfectly. My problem is, files without extentions, i.e. . ... (1 Reply)
Discussion started by: busillis
1 Replies

10. Shell Programming and Scripting

Editing File using awk/sed

Hello Awk Gurus, Can anyone of you help me with the below problem. I have got a file having data in below format pmFaultyTransportBlocks ----------------------- 9842993 pmFrmNoOfDiscRachFrames ----------------------- NULL pmNoRecRandomAccSuccess -----------------------... (4 Replies)
Discussion started by: Mohammed
4 Replies
Login or Register to Ask a Question