views:

127

answers:

3

I have a file as follows

  • 2.54 Ghz val
  • 2.53 Ghz val1
  • 1.6 Ghz val2
  • 800 Mhz val3
  • 2.54 Ghz val4
  • 2.53 Ghz val5
  • 1.6 Ghz val6
  • 800 Mhz val7

and the pattern continues ... I want to extract all 2.54 Ghz values in one file1 and all 2.53 Ghz values in another file2 , 1.60 Ghz values in file3 and 800 Mhz values in file4

can anyone help me with this ?

+7  A: 
 awk '{print $0 > "file_"$1"_"$2}' file
ghostdog74
Or possibly `...print $3 > ...` if only you only want the values and not the full line.
Dennis Williamson
yes, if OP only wants the values. I will leave it as $0 for the sake of generality.
ghostdog74
A: 

A grep/sed variation that gets the list of speed values to use as output filenames. This uses the rather brittle assumption that you can separate the speed from the value on 'hz space'... but you may be able to adapt this to your precise needs.

FILE=data.txt
sed -e 's/hz .*/hz/' < $FILE | sort -u | while read pat
do
    grep "$pat" $FILE | sed -e 's/.*hz //' > "$pat.txt"
done
martin clayton
A: 

Pure Bash:

rm --force file_*

while read speed magnitude value; do
    echo -e "${value}" >> "file_${speed}_${magnitude}"
done < file
fgm