Grepping a specific row from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grepping a specific row from a file
# 1  
Old 04-07-2013
Grepping a specific row from a file

Hi

I have output of a command saved in a file..
Code:
# cat /file.txt
  System: cu=4 ent=0.1 mode=on  
cu     min       u      s          w         i   
0      500    0.1  0.3  0.5   0.1   
1      200    0.5  0.2  0.3  0.0

By using ksh, what I need to do is, I need to grep the u,s,w and i fields of first row and sum them up and save them in a variable..

Eg., 0.1+0.3+0.5+0.1 values from first row and save them in a variable
0.5+0.2+0.3+0.0 values from second row and save them in other variable

Please help.. Smilie

Last edited by Franklin52; 04-08-2013 at 03:09 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 04-07-2013
Code:
awk '/^[0-9]/ {print "R"NR"="$3+$4+$5+$6}' file.txt

Code:
eval `
awk ...
`
echo $R3
echo $R4

# 3  
Old 04-07-2013
If cu increments, it may be a better number for the variable name than the record number

Code:
eval $(awk '/^[0-9]/ {print "R"$1"="$3+$4+$5+$6}' file.txt)

Code:
echo $R0
1
echo $R1
1

# 4  
Old 04-07-2013
Here is a way to do this entirely in ksh:
Code:
#!/bin/ksh
# Redirect input for following read commands
exec < file.txt
# Throw away header lines.
read x;read x
# Get 1st variable:
read x x u s w l
v1=$((u + s + w + l))
# Get 2nd variable:
read x x u s w l
v2=$((u + s + w + l))
printf "v1 = %.1f, v2 = %.1f\n" $v1 $v2

This will work for any Korn shell newer than November 1988. It won't work for ksh88 nor for bash nor for a Bourne shell (since none of them support floating point arithmetic and some of them don't support $((expression)) arithmetic expression expansions either.

With your sample input, the output produced is:
Code:
v1 = 1.0, v2 = 1.0

# 5  
Old 04-07-2013
Another similar approach using indexed arrays:
Code:
#!/bin/ksh

while read f1 f2 f3 f4 f5 f6
do
        [[ ! "$f1" == @([0-9]) ]] && continue

        (( ++c ))

        R[$c]=$(( f3 + f4 + f5 + f6 ))

done < file.txt

for i in ${!R[@]}
do
        printf "R[%d]: %.1f\n" $i ${R[$i]}
done

# 6  
Old 04-10-2013
As an alternative to the eval method above,
also a "here document" can set multiple shell variables from the command:
Code:
read R0 R1 << EOT
`awk '/^[0-9]/ {printf "%d ", $3+$4+$5+$6}' file.txt`
EOT
echo $R0
echo $R1

This works in every Bourne(-compatible) shell, because it automatically creates a tmp file.
But some shells have issues: create unsafe tmp files, or don't clean it up.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using awk to change a specific column and in a specific row

I am trying to change the number in bold to 2400 01,000300032,193631306,190619,0640,1,80,,2/ 02,193631306,000300032,1,190618,0640,CAD,2/ I'm not sure if sed or awk is the answer. I was going to use sed and do a character count up to that point, but that column directly before 0640 might... (8 Replies)
Discussion started by: juggernautjoee
8 Replies

2. Shell Programming and Scripting

Specific row match

Trying to match $1 of genematch.txt with $11 of design.txt, if there is a match then in the first row of the match $1 is copied to column 1, row 2 of design.txt, $2 is copied to column 2, row 2 of design.txt, $3 is copied to column 3, row 2 of design.txt, $4 is copied to column 4, row 2 of ... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

Search row by row from one file to another file if match is found print few colums of file 2

this is the requirement list.txt table1 table2 table3 testfile.txt name#place#data#select * from table1 name2#place2#data2#select * from table 10 innerjoin table3 name2#place2#data2#select * from table 10 output name place table1 name2 place table3 i tried using awk (7 Replies)
Discussion started by: vamsekumar
7 Replies

4. Shell Programming and Scripting

Printing next two lines from a file after grepping a specific pattern

Hi I have a file like # vi require.txt 1,BANK,Read blocks that cycle. yellow Read blocks. 2,ACCOUNT,Finished Red Finished . 3,LOAN, pipe white pipe 4,PROFIT,Resolve. black Resolve Am using like cat require.txt | grep -w ACCOUNTThe output I get is (8 Replies)
Discussion started by: Priya Amaresh
8 Replies

5. Shell Programming and Scripting

Subtracting each row from the first row in a single column file using awk

Hi Friends, I have a single column data like below. 1 2 3 4 5 I need the output like below. 0 1 2 3 4 where each row (including first row) subtracting from first row and the result should print below like the way shown in output file. Thanks Sid (11 Replies)
Discussion started by: ks_reddy
11 Replies

6. Shell Programming and Scripting

Sort a file from specific row onwards

Hello All: I've file in below format. File name is "FIRSTN.TBL": AAAAAA N BBBBBBBBBBBBBBBBBBBBBBB N . . . . ZZZZZZZZZZZZZZZZZZZZZZZZZZ N My file row length is 40 characters and my second column will start from 25th column and it is only... (3 Replies)
Discussion started by: nvkuriseti
3 Replies

7. UNIX for Dummies Questions & Answers

Grepping A Specific Column

Hello, I have a log file that outputs the data below. I would like to grep and display the data where column is equal '148.' I've searched the forum, and couldn't find any answers. I've tried all the grep switches and I get the same result as the log. I'm thinking I might have to use an... (4 Replies)
Discussion started by: ravzter
4 Replies

8. Shell Programming and Scripting

Insert a text from a specific row into a specific column using SED or AWK

Hi, I am having trouble converting a text file. I have been working for this whole day now, still i couldn't make it. Here is how the text file looks: _______________________________________________________ DEVICE STATUS INFORMATION FOR LOCATION 1: OPER STATES: Disabled E:Enabled ... (5 Replies)
Discussion started by: Issemael
5 Replies

9. Shell Programming and Scripting

Grepping for filenames containing value in specific segment within file

I am trying to grep for filenames containing a specific value within a particular segment. The lines containing the segment I'm looking through reads like "HL^1^^1^1", "10^9^9^0", and "HL^11^4^8^1". I would like to find the data that contains only the number nine after the third caret where the... (4 Replies)
Discussion started by: HLee1981
4 Replies

10. Shell Programming and Scripting

Changing the column for a row in a text file and adding another row

Hi, I want to write a shell script which increments a particular column in a row from a text file and then adds another row below the current row with the incremented value . For Eg . if the input file has a row : abc xyz lmn 89 lm nk o p I would like the script to create something like... (9 Replies)
Discussion started by: aYankeeFan
9 Replies
Login or Register to Ask a Question