Grep Examples in Unix

Simple grep

grep -il "ref" *201701*

Where “ref” is the reference I’m looking for. The 201701 is part of the filename so it’s easy to pull out messages for Jan ’17 for example or, say *2017012″ … all messages in 20-29th Jan 2017.

Good one if you want an invoice number and you have the recipient ID for example, try …

zgrep "BGM+38" `zgrep -il 5000119000006 *`

The `zgrep -il 5000119000006 *` runs first and produces a list of all filenames for that recipient, then the outer grep pulls out the BGM’s containing the invoice number.

Shell Script to move files from the /gis/data/error directory:

mv `grep -l OPUS *.err` tmp/opus
mv *Influe*.err tmp/influe
mv `grep -l SHPCON *.err` tmp/sap
mv `grep -l ORDERS01 *.err` tmp/sap 

How to UNZIP *.gz files

gunzip 1.3.5 (HP-UX fixlevel 1)(2002-09-30)

usage: gunzip [-cdfhlLnNrtvV19] [-S suffix] [file ...]
 -c --stdout      write on standard output, keep original files unchanged
 -d --decompress  decompress
 -f --force       force overwrite of output file and compress links
 -h --help        give this help
 -l --list        list compressed file contents
 -L --license     display software license
 -n --no-name     do not save or restore the original name and time stamp
 -N --name        save or restore the original name and time stamp
 -q --quiet       suppress all warnings
 -r --recursive   operate recursively on directories
 -S .suf  --suffix .suf     use suffix .suf on compressed files
 -t --test        test compressed file integrity
 -v --verbose     verbose mode
 -V --version     display version number
 -1 --fast        compress faster
 -9 --best        compress better
 file...          files to (de)compress. If none given, use standard input.
Report bugs to <bug-gzip@gnu.org>.
usoak059:/home/gbcsxf06> gunzip -f INBOUND_89696954_20161102115230.dat.gz
usoak059:/home/gbcsxf06> gunzip -f INBOUND_89697956_20161102120215.dat.gz

 

How to write script to MV files to sub-folder

#!/bin/ksh
cd /gis/data/error
grep -il "<STATUS_TENDERING>RJ</STATUS_TENDERING>" * | xargs -n1 -i mv {} /gis/data/error/expired
usoak059:/gis/data/archive/as2/KEU_ORBIT/i> zgrep -l 7080000000012 *201603*|xargs zgrep -l ORDER
INBOUND_63614497_20160307092650.dat.gz
INBOUND_63620453_20160307102217.dat.gz
INBOUND_63620456_20160307102217.dat.gz
INBOUND_63634324_20160307115652.dat.gz
INBOUND_64402957_20160314082700.dat.gz
INBOUND_64404393_20160314083700.dat.gz
INBOUND_65448045_20160321083623.dat.gz
INBOUND_65448046_20160321083623.dat.gz
INBOUND_66401366_20160330123749.dat
INBOUND_66402139_20160330124745.dat
usoak059:/gis/data/archive/as2/KEU_ORBIT/i> zmore INBOUND_63634324_20160307115652.dat.gz

------> INBOUND_63634324_20160307115652.dat.gz <------
usoak059:/gis/data/archive/as2/KEU_ORBIT/o> zgrep -l 3025710000109 *201609*|xargs zgrep -l DESADV
trans.84286380_20160908071228.dat.gz
trans.84286569_20160908071402.dat.gz
trans.84297688_20160908095710.dat.gz
trans.84442828_20160909175724.dat.gz
trans.84442986_20160909175850.dat.gz
usoak059:/gis/data/archive/as2/EU_XTEL_TPM/o> grep -l 92476782 *201610* | xargs grep -l INVOIC
OUTBOUND_INVOIC_86680142_20161003170441.dat

 

How to GREP and MOVE multiple files to /home/gbcsxf06

CD to where files you want are located.  Then perform the below CP and GREP command.

cd /gis/data/archive/opus/Bremen/i
cp `zgrep -l STW *20170106*` /home/gbcsxf06/

 

How Can I Grep For Two Different Strings and Print Them?

My data looks like this below:

STX=ANAA:1+5013546100917:KELLOGG COMPANY (GB) LIMITED+5000119000006:TESCO STORES PLC+160811:134338+63010+PIONEER+INVFIL+B'
MHD=1+INVFIL:9'
TYP=0700+INVOICES'
SDT=5013546100917:12191+KELLOGG COMPANY (GB) LIMITED+THE KELLOGG BUILDING:TALBOT ROAD:MANCHESTER::M16 OPU+151194288'
CDT=5000119000006:5000119000006+TESCO STORES PLC+BOUGHT LEDGER DEPARTMRENT:TESCO HOUSE:PO BOX 506:CARDIFF:CF4 4TS+220430231'
FIL=9476+1+160811'
FDT=160313+160315'
MTR=7'
MHD=2+INVOIC:9'
CLO=5000119008510:0100851:4420009+TESCO (CO ANTRIM)+KILBEGS ROAD:BALLYMENA ROAD:ANTRIM:AT:BT41 4NN'
IRF=92349489+160314+160314'

And I want to GREP for “FIL=” and “IRF=” and print them out to see the results.

zgrep -i 'FIL=\|IRF=\|' `zgrep -il "5000119000006" *201609*`
zgrep "FIL=|IRF=" `zgrep -il "50001190000006" *201609*'

I want the screen to show/print the following

FIL=9476+1+160811' and IRF=92349489+160314+160314'

Answer

If files are zipped:

zegrep 'FIL=|IRF=' *gz

If files are regular files (not zipped):

egrep 'FIL=|IRF=' *

So you should try like this:

First filter data with 50001190000006

If data present in file then search for FIL=|IRF= from the file

zgrep -q '5000119000006' test.file;if [ $? -eq 0 ];then egrep 'FIL=|IRF=' test.file; fi

FIL=9476+1+160811′

IRF=92349489+160314+160314′

Replace test.file with 201611 (actual file name in your system)

 

How to perform a replace in files

File ready to get processed under ‘/int/prod/gis/data/keu/as2/KEU_TRANSP/i’

Replaced using command

grep -l '<DOCREL>701</DOCREL>' * | xargs sed -i 's/DOCREL>701</DOCREL>731</g'

-rw-rw-r-- 1 cleoadmin mft 25173 Jan 24 15:00 trnspn_Accepted_0006191142_20170123124508396_HY1157-ET0171_20170123-114518.xml
-rw-rw-r-- 1 cleoadmin mft  5066 Jan 24 15:00 trnspn_Accepted_0006191246_20170124092047683_HY1158-NC9061_20170124-082059.xml
-rw-rw-r-- 1 cleoadmin mft  5119 Jan 24 15:00 trnspn_Accepted_0006191247_20170123114112351_HY1157-ET0171_20170123-104120.xml
-rw-rw-r-- 1 cleoadmin mft  5039 Jan 24 15:00 trnspn_Accepted_0006191547_20170123115608736_HY1157-ET0171_20170123-105614.xml
-rw-rw-r-- 1 cleoadmin mft  6908 Jan 24 15:00 trnspn_Timeslot_0006190288_20170123112015862_HY1158-NC9061_20170123-102020.xml
-rw-rw-r-- 1 cleoadmin mft  6029 Jan 24 15:00 trnspn_Timeslot_0006190686_20170123150010656_HY1157-ET0171_20170123-140019.xml

How to purge files older than 7 days

usphi556:/gis/data/dev/sap/EAD/out> find . -mtime +7 | wc
13830 13830 345721

How to Search Files & Move Them

#!/bin/ksh
cd /gis/data/error
grep -il "<STATUS_TENDERING>RJ</STATUS_TENDERING>" * | xargs -n1 -i mv {} /gis/data/error/expired

The above grep rule will search for values within the quotes and performs a move to the named folder.

 

Click below to find out more what Appleyard’s EDI Services can do for you

Leave a Reply

Your email address will not be published. Required fields are marked *

*