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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replace awk with a perl one liner (REGEXP and FS)
# 1  
Old 02-03-2012
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:
Code:
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 :
Code:
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

Moderator's Comments:
Mod Comment Please use next time code tags for your code and data

Last edited by vbe; 02-03-2012 at 12:47 PM..
# 2  
Old 02-03-2012
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
Code:
#!/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.
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 02-03-2012
Quote:
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.
hmm, what should it do, when i want to use a shell script at different os versions (HP-UX, Linux (SuSe / RedHat ).

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

i thought "perl" was a good option.

regards

Last edited by Franklin52; 02-04-2012 at 02:20 PM.. Reason: Please use code tags for data and code samples, thank you
# 4  
Old 02-03-2012
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
Code:
awk -F# '$1 ~ search_string { print $1,$2,$3,$4 }' search_string="${search_string}" file


Last edited by Corona688; 02-03-2012 at 05:04 PM..
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 02-03-2012
If you still insist on perl, here's one.
Code:
$ 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?
Code:
$ grep $search_string inputfile | cut -d'#' -f2- | tr '#' ' '
field1 field2 junk junk junk junk


Last edited by balajesuri; 02-03-2012 at 09:42 PM..
This User Gave Thanks to balajesuri For This Post:
# 6  
Old 02-10-2012
Quote:
Originally Posted by jim mcnamara
Ok, but be aware: perl is NOT POSIX so it is not guaranteed to be on a given unix box.
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 ----------

Quote:
Originally Posted by Corona688
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?
Code:
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
Code:
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

find and Replace String in Perl - Regexp

Trying to find and replace one string with another string in a file #!/usr/bin/perl $csd_table_path = "/file.ntab"; $find_str = '--bundle_type=021'; $repl_str = '--bundle_type=021 --target=/dev/disk1s2'; if( system("/usr/bin/perl -p -i -e 's/$find_str/$repl_str/' $csd_table_path")... (2 Replies)
Discussion started by: cillmor
2 Replies

2. UNIX for Dummies Questions & Answers

What awk 1-liner will replace value in 1stField of a delimited file with the value of '5' ?

Hi, I am a newbie to awk. Here is my problem. Looking for an awk 1-liner to solve it: My Computing Environment: - Solaris10 - I prefer to use csh or sh shells 1. Lets say my input file is File1.dat (delimter = | ) and looks as follows: (File1.dat) ... (1 Reply)
Discussion started by: andy b
1 Replies

3. UNIX for Dummies Questions & Answers

Perl one liner to replace text

Not quite a unix question but problem in a perl command. Taking a chance if someone knows about the error cat 1 a b c d perl -p -e 's/a/b/g' 1 b b c d What is the problem here?? perl -p -i -e 's/a/b/g' 1 Can't remove 1: Text file busy, skipping file. (2 Replies)
Discussion started by: analyst
2 Replies

4. Shell Programming and Scripting

awk, sed or perl regexp to print values from file

Hello all According to the following file (orignal one contains 200x times the same structure...) I was wondering if someone could help me to print <byte>??</byte> values example, running this script/command like ./script.sh xxapp I would expect as output: 102 116 112 ./script.sh xxapp2... (2 Replies)
Discussion started by: cabrao
2 Replies

5. Shell Programming and Scripting

Replacing Awk with One-liner Perl

can someone help me translate the following command, from: /usr/bin/awk "/^$TOFDAYM $TOFDAYD /,0" $LOGFILE to something like perl -e ..... basically, i want to use perl to do awk functions within a shell script. i want to do the above awk, using perl. any suggestions? (9 Replies)
Discussion started by: SkySmart
9 Replies

6. Shell Programming and Scripting

Need an awk / sed / or perl one-liner to remove last 4 characters with non-unique pattern.

Hi, I'm writing a ksh script and trying to use an awk / sed / or perl one-liner to remove the last 4 characters of a line in a file if it begins with a period. Here is the contents of the file... the column in which I want to remove the last 4 characters is the last column. ($6 in awk). I've... (10 Replies)
Discussion started by: right_coaster
10 Replies

7. Shell Programming and Scripting

Advanced AWK Regexp substring to int & Replace

Hi! I have a difficult problem, to step up a unknown version number in a text file, and save the file. It would be great to run script.sh and the version gets increased. Example the content of the textfile.txt hello version = x bye This include three steps 1. First find the char after... (2 Replies)
Discussion started by: Beachboy72
2 Replies

8. Shell Programming and Scripting

Search & Replace regex Perl one liner to AWK one liner

Thanks for giving your time and effort to answer questions and helping newbies like me understand awk. I have a huge file, millions of lines, so perl takes quite a bit of time, I'd like to convert these perl one liners to awk. Basically I'd like all lines with ISA sandwiched between... (9 Replies)
Discussion started by: verge
9 Replies

9. Shell Programming and Scripting

awk multiple-line search and replace one-liner

Hi I am trying to search and replace a multi line pattern in a php file using awk. The pattern starts with <div id="navbar"> and ends with </div> and spans over an unknown number of lines. I need the command to be a one liner. I use the "record separator" like this : awk -v... (8 Replies)
Discussion started by: louisJ
8 Replies

10. Shell Programming and Scripting

awk/perl one-liner assist

In a ~4GB file there are lines like, 13.13.4.3 Googe.com - Jan/23/2011:00:00:00 +0000 "URL Google HTTP/1.1" 45 56 208 - "http://www.gogle.com/webhp?hl=en&tab=nw#hl=en&source=hp&biw=1366&bih=667&q=hello&aq=f&aqi=&aql=&oq=&fp=c432485467934a89" ".Net; Fox" - 13.145.3.3 Goge.com -... (3 Replies)
Discussion started by: gameboy87
3 Replies
Login or Register to Ask a Question