replace awk with a perl one liner (REGEXP and FS)

hello,
I want to replace awk with a perl one liner in unix.
i use in awk REGEX and FS ( field separator) because
awk syntaxes in different unix os versions have not the same behaviour.

Awk, Nawk and GNU Awk Cheat Sheet - good coders code, great reuse

i have a file named "file" and want search the exact value (example: search_value) of the first entry (FS="#" ). but there can be some characters at the beginning (REGEXP)

file:
;;search_value#field1#field2#junk#junk#junk#junk

UNIX: AWK in a KSH script:

search_string="search_value"
awk -v search_string="${search_string}" -F# '$1 ~ (+search_string) { print $1,$2,$3,$4 }' file 

this works not at whole unix os versions, so i have develope a perl one liner ( i have mininamal perl know how,i use examples of the net ):

UNIX: PERL in a KSH script :

search_string="search_value" \
 perl -l -a -n -F"#" -e  'print $F[1] if ($F[0] =~ /^\S+$ENV{"search_string"}$/ ) ' file      

is it ok ? problem is to put the unix shell variable to perl .

regards

Ok, but be aware: perl is NOT POSIX so it is not guaranteed to be on a given unix box.

Next, there are different awk versions on some machines - KSH by itself does not enforce a particular version of awk. So simply using

#!/bin/ksh

does not guarantee anything about awk.

Systems can have aliases: awk for GNU gawk, for example. Solaris has an ancient version of awk: /usr/bin/awk plus others: /usr/bin/nawk (what you want to use)

Circumventing the portability problem by assuming perl is there is not necessarily a good option.

My point: you may be wasting your time if this has to be freely portable.

1 Like

hmm, what should it do, when i want to use a shell script at different os versions (HP-UX, Linux (SuSe / RedHat ).

like :

case "$(uname)" in
   HP-UX ) .... awk 
   Linux   ) .... awk
esac

i thought "perl" was a good option.

regards

Improve your awk script, I'd say... Surely it's possible to make it portable, if it isn't already. The only thing I see which might not be completely portable is the -v. In what way does it not work, and on what particular platform?

What does (+search_string) mean, anyway?

Try

awk -F# '$1 ~ search_string { print $1,$2,$3,$4 }' search_string="${search_string}" file
1 Like

If you still insist on perl, here's one.

$ cat inputfile
;;search_value#field1#field2#junk#junk#junk#junk
$ perl -F"#" -lane '$e=shift @F;if($e=~ /^\S+'$search_string'/){print "@F"}' inputfile
field1 field2 junk junk junk junk

By the way, a simple shell one-liner should do, don't you think?

$ grep $search_string inputfile | cut -d'#' -f2- | tr '#' ' '
field1 field2 junk junk junk junk
1 Like

this is new for me but for me issue : the behaviour of REGEXP is the same at all os versions? right ? i have to test it !

---------- Post updated at 03:20 PM ---------- Previous update was at 03:03 PM ----------

What does (+search_string) mean, anyway?

it is here a example, some i want to search with strings that includes "/" ( like filesystems) . so i use -v . but when i have it in a "awk" variable, it isn't easy to use REGEXP
Try

awk -F# '$1 ~ search_string { print $1,$2,$3,$4 }' search_string="${search_string}" file

(+search_string) : i want to match zero or more occurrences before the "search_string" like the REGEXP of "grep" .*

how i use REGEXP for awk for following examples :

+++++++++++++++++++++++++++++++++++++++++++++
easy:
awk -F# '$1 ~ search_string { print $1,$2,$3,$4 }' file:
search_string="search_value"

i search with example above , but not including in search "+"
file:
;;search_value1#field1#field2#junk#junk#junk#junk
;;search_value2#field1#field2#junk#junk#junk#junk

+++++++++++++++++++++++++++++++++++++++++++++
search_string="search_value1"
i want to search only "search_value1"

file:
;;search_value1#field1#field2#junk#junk#junk#junk
;;search_value11#field1#field2#junk#junk#junk#junk

+++++++++++++++++++++++++++++++++++++++++++++
search_string="search_value1"
i want to search all with "search_value1*" (example: search_value1,search_value11,search_value111, not search_value121)

file:
;;search_value1#field1#field2#junk#junk#junk#junk
;;search_value11#field1#field2#junk#junk#junk#junk
;;search_value111#field1#field2#junk#junk#junk#junk
;;search_value121#field1#field2#junk#junk#junk#junk

regards