I've run the command

grep -a --binary-file=text "DLDM" /home/path/ldm.log-$1* | grep -a "$line"

and the output is something like this :-

/home/path/ldm.log2015:"unix","20150518 11:36:09",DLDM,CUST_PP,"unix%unix@pp",11,0,,"7A104802E728","4529800000","123456789",0,0,0,0,3,"75126BFC5DA0|548B0F66882B|250E4473F3C6",,0,"COUNTRY_CODE",21,5769,15,0,1,"CURRENCY",0.0255,1,,,,"","","001",6,"20150518 11:36:09","",""

I want to print only specific columns, like

20150518 11:36:09 , 7A104802E728 , 4529800000 , 123456789 , CURRENCY

and if possible can I change CURRENCY to something else (eg: META) as string in the output?


could you use awk, at least in place of the second grep? Something like

grep -a --binary-file=text "DLDM" /home/path/ldm.log-$1* |  awk -F, "/$line/ {print \$2 \",\" \$9 \",\" \$10 \",\" \$11 \",META\"}"

Note all the escaping is so you can use $line in the pattern, if you don't need it to be a variable you could use single quotes for the outer quotes and ignore most of the interior quoting.

To replace the string CURRENCY with META change the awk to be as follows:

grep -a --binary-file=text "DLDM" /home/path/ldm.log-$1* | awk -F, "/$line/ {gsub(\"CURRENCY\", \"META\", \$26);print \$2 \",\" \$9 \",\" \$10 \",\" \$11 \",\" \$26}"
  • the META columns can I can it to different output based on different input? CURRENCY to META, CURRENCY01 to META01, etc.. – Asif May 19 '15 at 3:00
  • @asif sorry for the delay, solution for replacing CURRENCY with META is now in the answer – Eric Renouf May 19 '15 at 15:21

Use cut to extract the columns you need (providing the column numbers for us would have been a kindness), and sed to alter the text:

grep ... | cut -d, -f2,9,10,11,26 | sed -e 's/CURRENCY/META/' -e 's/"//g'


20150518 11:36:09,7A104802E728,4529800000,123456789,META

Another approach, since your input data looks like well-formed CSV (ignoring the part before the first colon), is to use a language with a proper CSV parser

grep ... | ruby -rcsv -ne '
    csv_text = $_[/(?<=:).+/]           # ignore up to the first colon
    row = CSV.parse_line csv_text       # parse
    out = row.values_at(1,8,9,10,25)    # extract your fields
    out[-1]["CURRENCY"] = "META"        # text replacement of last field
    puts CSV.generate_line out          # output                       

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.