Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Multi pattern selection

Shell Programming and Scripting


Tags
solved, solved awk

Reply    
 
Thread Tools Search this Thread Display Modes
    #8  
Old Unix and Linux 07-12-2017
drl's Unix or Linux Image
drl drl is offline Forum Advisor  
Registered Voter
 
Join Date: Apr 2007
Last Activity: 20 September 2017, 3:01 PM EDT
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 2,166
Thanks: 219
Thanked 400 Times in 345 Posts
Hi.

Using standard commands, and the very small sample data set; the operative commands here are:

Code:
grep -E 'SName|ESys' $FILE |
paste - -

In this script and output:

Code:
#!/usr/bin/env bash

# @(#) s1       Demonstrate matching, joining.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
em() { pe "$*" >&2 ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C grep paste pass-fail

FILE=${1-data1}
E=expected-output.txt

pl " Input data file $FILE:"
cat $FILE

pl " Expected output:"
cat $E

pl " Results:"
grep -E 'SName|ESys' $FILE |
paste - - | tee f1

pl " Verify results if possible:"
C=$HOME/bin/pass-fail
[ -f $C ] && $C || ( pe; pe " Results cannot be verified." ) >&2

exit 0

producing:

Code:
$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.8 (jessie) 
bash GNU bash 4.3.30
grep (GNU grep) 2.20
paste (GNU coreutils) 8.23
pass-fail (local) 1.9

-----
 Input data file data1:
SName=abc123
ESys=xyz456
Host=xxx
Port=yyy

-----
 Expected output:
SName=abc123  ESys=xyz456

-----
 Results:
SName=abc123    ESys=xyz456

-----
 Verify results if possible:

-----
 Comparison of 1 created lines with 1 lines of desired results:
f1 expected-output.txt differ: char 13, line 1
 Failed -- files f1 and expected-output.txt not identical -- detailed comparison follows.
 Succeeded by ignoring whitespace differences.

Best wishes ... cheers, drl
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Multi-line filtering based on multi-line pattern in a file Finja Shell Programming and Scripting 2 05-16-2012 10:42 AM
Print between multi line pattern sol_nov Shell Programming and Scripting 3 02-27-2012 05:41 PM
Sed or awk : pattern selection based on special characters usha rao Shell Programming and Scripting 6 09-13-2011 12:30 PM
how can awk match multi pattern in a string sleepy_11 UNIX for Advanced & Expert Users 5 03-16-2008 11:40 PM



All times are GMT -4. The time now is 03:58 PM.