Reading in 4th number in a line

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading in 4th number in a line
# 1  
Old 11-06-2013
[Solved] Reading in 4th number in a line

I'd like to read a file (mydata.dat) line by line. The file consists of 5 columns filled with numbers, like so:
    83.018     1.953    49.587   20550.000  353
    83.213     1.953    49.195   20600.000  171
    84.935     1.954    48.803   20650.000  920

For every read line (i.e. in every step of the loop), I'd like to read the 4th number in that line (e.g. 20550.000 in the first line above) into a variable arg4. The first, second and third numbers (e.g. 83.018, 1.953, 49.587) I'd like to write to a file (outfile_${arg4}) whose name should contain the 4th number.

E.g. file 'outfile_20550.000' contains
83.018     1.953    49.587

This is what I came up with, but it doesn't work yet..
while read line;
      arg4=`echo  | awk '{ print $4 }' $line`
      echo | awk `{ print $1, $2, $3 }` $line >> outfile_${arg4}   

done < mydata.dat

Moderator's Comments:
Mod Comment Please use code tags for your data and code next time Smilie
# 2  
Old 11-06-2013
while read a b c d rest
    echo "$a $b $c" >> outfile_$d
done < mydata.dat

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 11-06-2013
Beautifully simple! Thanks a lot :-)
I'm new to shell scripting and very much appreciate your help.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reading from a dynamically decided line number

Hi All, We get a file from our client for processing everyday. We have to start reading the file from the line after the line that says "version=". My idea was to grep and find the line number(say 'n') of the line with "version=" and start reading from the (n+1)th line. Can anyone please guide... (7 Replies)
Discussion started by: jerome_rajan
7 Replies

2. Shell Programming and Scripting

Write $line number into textfile and read from line number

Hello everyone, I don't really know anything about scripting, but I have to manage to make this script, out of necessity. #!/bin/bash while read -r line; do #I'm reading from a big wordlist instructions using $line done Is there a way to automatically write the $line number the script... (4 Replies)
Discussion started by: bobylapointe
4 Replies

3. Shell Programming and Scripting

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies

4. UNIX for Dummies Questions & Answers

How to read contents of a file from a given line number upto line number again specified by user

Hello Everyone. I am trying to display contains of a file from a specific line to a specific line(let say, from line number 3 to line number 5). For this I got the shell script as shown below: if ; then if ; then tail +$1 $3 | head -n $2 else ... (5 Replies)
Discussion started by: grc
5 Replies

5. UNIX for Dummies Questions & Answers

retrieve every 4th line from a file

Hello, I want to retrieve 2, 6, 10, 14...... (each 4 lines apart) from a file that looks like the sample below. In other words, I want only lines corresponding to the Xs. Header1_a XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Header1_b yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy Header2_a... (2 Replies)
Discussion started by: Gussifinknottle
2 Replies

6. Shell Programming and Scripting

how to get the data from line number 1 to line number 100 of a file

Hi Everybody, I am trying to write a script that will get some perticuler data from a file and redirect to a file. My Question is, I have a Very huge file,In that file I have my required data is started from 25th line and it will ends in 100th line. I know the line numbers, I need to get all... (9 Replies)
Discussion started by: Anji
9 Replies

7. UNIX for Dummies Questions & Answers

query on how to search for a line and read 4th word from that line

Assume I have a text file as below: me con pi ind ken pras ur me con rome ind kent pras urs pintu con mys ind pan pras ki con kit ind sys My requirement, I need to search for "con rome" and if exists, then print 4th word from rome, i.e in above example, since "con rome"... (4 Replies)
Discussion started by: jaggesh
4 Replies

8. Shell Programming and Scripting

Adding a columnfrom a specifit line number to a specific line number

Hi, I have a huge file & I want to add a specific text in column. But I want to add this text from a specific line number to a specific line number & another text in to another range of line numbers. To be more specific: lets say my file has 1000 lines & 4 Columns. I want to add text "Hello"... (2 Replies)
Discussion started by: Ezy
2 Replies

9. Shell Programming and Scripting

Appending line number to each line and getting total number of lines

Hello, I need help in appending the line number of each line to the file and also to get the total number of lines. Can somebody please help me. I have a file say: abc def ccc ddd ffff The output should be: Instance1=abc Instance2=def Instance3=ccc Instance4=ddd Instance5=ffff ... (2 Replies)
Discussion started by: chiru_h
2 Replies
Login or Register to Ask a Question