Awk script into Perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk script into Perl
# 1  
Old 04-02-2010
Awk script into Perl

Hello,

I have not programmed in Perl, but maybe someone can help me or point me to other links. I have searched for and found a solution to my initial problem. I have a text file of data where I want to search for a particular string but return the prior line. I found out here something that worked great (with a small test file)...

Code:
cat myfile.txt | awk '{ x[NR] = $0 } END { for ( i=1; i<=NR; i++ ) { if (x[i] ~ /my_search_string/ ) print x[i-1] }} ' >> $filedir/$myoutput

However, I did not realize there was a 3000 character limit on the awk command, so when I input an extremely wide file (like 50,000+ characters in 1 line), this is failing. So, everyone said to switch to gawk/GNU awk. We do not have gawk here, and for security reasons, I do not have permission to download or install gawk on our HP-UX and/or Sun Solaris boxes.

Can this be easily re-written into a Perl script? And can Perl handle extremely wide files?

Thanks,
Brian

Last edited by pludi; 04-02-2010 at 02:32 PM.. Reason: code tags, please...
# 2  
Old 04-02-2010
Am I getting you right, if the string you're searching for is in line 43, you want to print line 42, right? If so (untested):
Code:
awk 'BEGIN{prev=""} /search_string/{print prev} {prev=$0}' myfile.txt
perl -ne 'print $prev if /search_string/; $prev=$_;' myfile.txt

For more complex stuff, take a look at a2p, part of any Perl distribution.
# 3  
Old 04-02-2010
Quote:
Originally Posted by bsp18974
...
I have a text file of data where I want to search for a particular string but return the prior line.
...
If you want to return just one prior line using Perl, then pludi's Perl script is extremely efficient.

Here's a variation though, but the underlying concept is the same - print variable p when string matches and then save the current line to variable p.

Code:
$
$ cat -n f5
     1  aaaaaaa THIS IS LINE_1 aaaaaaa
     2  bbbbbbb THIS IS LINE_2 bbbbbbb
     3  ccccccc THIS IS LINE_3 ccccccc
     4  ddddddd THIS IS LINE_4 ddddddd
$
$ perl -lne '/LINE_3/ && print $p; $p=$_' f5
bbbbbbb THIS IS LINE_2 bbbbbbb
$
$

By the definition of your problem, it won't print anything if you match "LINE_1" in the file above.
And you'll never be able to print the last line either.

Code:
$
$ perl -lne '/LINE_1/ && print $p; $p=$_' f5
$
$ perl -lne '/LINE_4/ && print $p; $p=$_' f5
ccccccc THIS IS LINE_3 ccccccc
$

Quote:
... when I input an extremely wide file (like 50,000+ characters in 1 line), this is failing.
...
I created a dummy file very similar to f5 above, but with each line containing 1,000,000 characters (1 million). And the script worked fine. Even with the search string being at the very end of the 1-million character line.

Quote:
Can this be easily re-written into a Perl script?
I'll leave it for you to decide. Smilie

Quote:
And can Perl handle extremely wide files?
The idea here is to save the entire line in a single Perl variable ($p above or $prev in pludi's script).
Perl imposes no limitation on the length of a string variable. It is limited only by your system's memory.

Here's an excerpt from the Perl man page:

Code:
...
Unlike most Unix utilities, Perl does not arbitrarily limit the size of your data--if you've got the memory,
Perl can slurp in your whole file as a single string.
...

HTH,
tyler_durden
# 4  
Old 04-06-2010
Tyler_durden and pludi,

Thanks for your replies. This is working for me now.

Thanks,
Brian
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script in Perl or awk to remove multiple hyphens

Dear all, I have a database of compound words. I want to retain only strings with a single hyphen and identify those strings which have more than one hyphen. I am giving an example below test-test test-test-test test-test-test-test-test good-for-nothing The regex/script should remove all... (11 Replies)
Discussion started by: gimley
11 Replies

2. Shell Programming and Scripting

awk or perl script for preposition splitter

Hello, I am writing a Natural Language Parser and one of the tools I need is to separate prepositional phrase markers which begin with a Preposition. I have a long list of such markers (sample given below)and am looking for a script in awk or perl which will allow me to access a look-up file... (2 Replies)
Discussion started by: gimley
2 Replies

3. Shell Programming and Scripting

Multi platform script perl or awk

Hi gurus, I am trying to match records in following format: (-,username,domain1.co.uk)\ (-,username,domain2.co.uk) either awk or perl must be used. I am using cygwin. I wrote following code which works and matches both above entries: awk 'BEGIN {musr="(-,username,+.co.uk)"} {if... (8 Replies)
Discussion started by: wakatana
8 Replies

4. Shell Programming and Scripting

Help with convert awk script into perl

Input file (a list of input file name with *.txt extension): campus.com_icmp_ping_alive.txt data_local_cd_httpd.txt data_local_cd.txt new_local_cd_mysql.txt new_local_cd_nagios_content.txt Desired output file: data local_cd_httpd data local_cd new local_cd_mysql new ... (9 Replies)
Discussion started by: perl_beginner
9 Replies

5. Shell Programming and Scripting

usage of AWK command under perl script

i have two files as shown below t1.txt: argument1 argu2 argu37 t2.txt: 22 33 44 i want o/p as argument1 22 argu2 33 argu37 44 i am trying to merge two file under perl script using following system("paste t1.txt t2.txt | awk... (3 Replies)
Discussion started by: roopa
3 Replies

6. Shell Programming and Scripting

Shell script (not Perl) to parse xml with awk

Hi, I have to make an script according to these: - I have couples of files like: xxxxxxxxxxxxx.csv xxxxxxxxxxxxx_desc.xml - every xml file has diferent fields, but keeps this format: ........ <defaultName>2011-02-25T16:43:43.582Z</defaultName> ........... (2 Replies)
Discussion started by: Pluff
2 Replies

7. Shell Programming and Scripting

Executing AWK in a perl script using 'system'...

I have a simple perl script that looks similar to this: #!/usr/bin/perl/ # Have a lot of PERL code in the front of this script. #Would now like to execute a system command using AWK system (qq(cd /location && awk '/full/ {print $1;exit}' /myfile)); The system command in my perl script... (4 Replies)
Discussion started by: SysAdm2
4 Replies

8. Shell Programming and Scripting

awk script in perl

Hi Linux users, I have to convert a shell script in a perl script! The command takes two files (two tables) and compares them to find the same values in 4 columns ($2" "$3" "$8" "$9) and prints out only the common lines. This is the command: cat first_file.txt | while read i; do cat... (2 Replies)
Discussion started by: m_elena
2 Replies

9. Shell Programming and Scripting

perl as awk replacement in a script.

Hey all, Im trying to write a script on windows, which Im not too familiar with. Im generally a bash scripting guy but am using perl for this case. My question is... I have this exact output: 2 Dir(s) 6,380,429,312 bytes free and I just need to get the number out... (4 Replies)
Discussion started by: trey85stang
4 Replies

10. Shell Programming and Scripting

embeding awk script in perl program

Collegues I have an AWK script like the following. { if ($2 ~ /JJ/ && $4 ~ /IN/) { print $2, $3, $4, $5 } } How can I embed it in a perl program. Jaganadh.G (5 Replies)
Discussion started by: jaganadh
5 Replies
Login or Register to Ask a Question