terminal - How to append a value at the end of each line obtained with grep -


i have csv files parse grep (or other function terminal) in order extract informations. in form:

* comment 1 * comment line 2 explaining following numbers mean 1000000 ; 3208105 ; 0.18 ; 0.45 ; 0.00015 ; 0.1485 ; 0.03 ; 1 ; 1 ; 5 ; 477003 ;   * comment 3 * comment 4 explaining meaning of following lines  * comment 5 0; 706520; p; 30.4983 1; 20859; p; 57.8 2; 192814; p; 111.842 3; 344542; p; 130.543 4; 54605; p; 131.598 5; 64746; d; 140.898 6; 442082; p; 214.11 7; 546701; p; 249.167 8; 298394; p; 305.034 9; 81188; p; 305.034 ....... 

in each file there @ 1 line in third field equal d instead of p. either there line containing d or there none.

i have lot of files , extract each file line (if present) containing letter d , append after line last parameter of first not-comment line, in example 47703.

up managed extract separately lines need.

with can extract every line containing d every file have:

grep -h -e ' d;' *.csv > output.csv 

and can extract number 47703 file 1 in example:

grep -v -e "^*" -e " p; " -e " d; " example_file.csv | cut -d \; -f 11 

but don't know how put these 2 together.

the final output obtain example @ beginning single line this:

5; 64746; d; 140.898; 47703 

and have line every csv file in current directory.

is there way this?

i used loop loop .csv files , assign returned values greps variables, concatenated @ end of each loop echoed:

for f in *.csv ; value=`grep -v -e "^*" -e " p; " -e " d; " -e '^\s*$' "$f" | cut -d \; -f 11` ; line=`grep -h -e ' d;' "$f" ; echo "$line;$value" ; done

edit: (i added -e '^\s*$' first grep, gets line values on first not commented line. before, matched empty lines)

this echoes lines 5; 64746; d; 140.898; 47703, wanted. if want redirect file (all found lines in single output file), can add last echo in long command, like:

for f in *.csv ; value=`grep -v -e "^*" -e " p; " -e " d; " -e '^\s*$' "$f" | cut -d \; -f 11` ; line=`grep -h -e ' d;' "$f" ; echo "$line;$value" > output.csv ; done

for readability, same code on multiple lines:

for f in *.csv      value=`grep -v -e "^*" -e " p; " -e " d; " -e '^\s*$' "$f" | cut -d \; -f 11`     line=`grep -h -e ' d;' "$f"     echo "$line;$value" done 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -