Unix Search (grep) -


i new unix , know command search words in file through grep.

with command

'grep star file.txt | grep ptext | grep snum  > results.txt' 

the grep command return following content includes other details tag along same line.

star=20140201 14:01:05|ptext=sample1|subm=retapp@s01gretcd1|sbid=retapp|snum=232356|.... star=20140201 14:02:05|ptext=sample2|subm=retapp@s01gretcd1|sbid=retapp|snum=556677|... star=20140201 14:03:05|subm=retapp@s01gretcd1|sbid=retapp|snum=768764|.... star=20140201 14:03:05|ptext=sample3|subm=retapp@s01gretcd1|sbid=retapp|snum=768764|.... 

is there way results follow:

star=20140201 14:01:05|ptext=sample1|snum=232356|.... star=20140201 14:02:05|ptext=sample2|snum=556677|... star=20140201 14:03:05|ptext=sample3|snum=768764|.... 

results expected : rows includes 3 variables without other redundant data

thank you

use awk , select columns need. set input , output field separator | data delimited that. once line split, pick columns need. $1 contain column1, $2 contain column2 , on ...

awk 'begin{fs=ofs="|"}{print $1,$2,$5}' file.txt 

use perl if not sure columns contain data.

perl -f'\|' -lane 'print join "|", grep { /star|ptext|snum/ } @f' file.txt 

inside grep using simple regex match of 3 specified string. can alter them deem fit.

update based on new requirement:

perl -f'\|' -lane 'print join "|", grep { /star|ptext|snum/ } @f if /snum/ && /ptext/ && /star/' file.txt 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -