Multiple string input in a awk


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Multiple string input in a awk
# 1  
Old 11-22-2014
Blade Multiple string input in a awk

Hi everybody,
I just start my learning about Linux.
So, if you can help me, it would be very good !

I have already find the possibility to find the position of a character regular expression in a line with the help of awk :
test.txt is : AAAAAGHIJKLAjKMEFJKLjklABCDJkLEFGHIJKL
My script is like this :

Code:
awk -f findstring.awk test.txt > testreturn.txt

And my findstring.awk is like this :
Code:
BEGIN{ SLENGTH = 3 }
{
    string = "jkl"
    skipped = 0
    starts = ""
    while ( SSTART = index($0,string )) {
        starts = starts (starts?" ":"") (skipped + SSTART)
        $0 = substr($0,SSTART + SLENGTH)
        skipped += (SSTART + SLENGTH - 1)
    }
}
starts { print starts }

The command returns 21, as expected (beginning of the string "jkl").
With string = "JKL", the command returns 9 18 36, as expected also.

But, I'm not able to apply the same system with the same .awk to find not only "jkl" regular expression, but also in the same time "JKL" and "JkL".
By extension, the goal is to find a regular expression without case sensitivity, and also able to find "jKM" (with a variability at one or more positions in this same regular expression).

I really want to thank you before your answers.
Best.
# 2  
Old 11-22-2014
You will have to convert both the search string and the input line to either upper or lower case. For example:
Code:
BEGIN{ SLENGTH = 3 }
{
    string = "jkl"
    skipped = 0
    starts = ""
    line   = tolower($0);
    while ( SSTART = index(line,string )) {
        starts = starts (starts?" ":"") (skipped + SSTART)
        line = substr(line,SSTART + SLENGTH)
        skipped += (SSTART + SLENGTH - 1)
    }
}
starts { print starts }

which returns:
Code:
9 18 21 28 36

# 3  
Old 11-22-2014
Thannnkkksss !

Youuuuhhhouuuhhhouu !!!
Thanks a lot for your fast answer.
Really, I thank you a lot !
It's work very well.
Well, have a good end of week-end !
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk command input string too long, limit

cat filename| awk '{ $1=""; print $0}' in my file there are few lines that has more than 3000 characters per line and as soon as I run the above command it cores, strings core reveals that the awk is failing because input string too long, limit. can i get some help from the experts to find... (8 Replies)
Discussion started by: knijjar
8 Replies

2. Shell Programming and Scripting

Enhance existing script: Extract Multiple variables & Input in an echo string

Hi Experts I need your help to optimize my script to execute better as I have nearly 1M records & the script is taking close to 40 minutes to execute, so would need support on a faster alternative. Input: file {"house":"1024","zip":"2345","city":"asd","country":"zzv"}... (2 Replies)
Discussion started by: nk1984
2 Replies

3. Shell Programming and Scripting

Grep multiple string from input

How can I grep multiple strings from an input, strings are separated by space. Strings to grep: 7680FR 6791HH 1234AA Input: AA 7680FR AA 6891HH AA 6791UA BB 9834HA BB 1434AB DD 1234AA DD 6991HH DD 6791HH Required output: AA 7680FR (9 Replies)
Discussion started by: aydj
9 Replies

4. Shell Programming and Scripting

Multiple results as input to grep or awk

Hi: This is my first post in this forum. apologies if I am asking question that has been asked here multiple times. I wrote a python script that prints list of identifiers. I have another file notoriously big and with no particular format. I plan to search the identifiers in this file... (2 Replies)
Discussion started by: oriolebaltimore
2 Replies

5. Shell Programming and Scripting

FOR loop with multiple files as input and awk

Hi all , i want to pass multiple files as input to a for loop for i in file1 file2 file3 do some awk action < $i >> $i.out done but im getting error in that for loop is the way i use to pass files to awk using for correct and 2.we can directly pass multiple files to awk as... (7 Replies)
Discussion started by: zozoo
7 Replies

6. Shell Programming and Scripting

awk + gsub to search multiple input values & replace with located string + extra text

Hi all. I have the following command that is successfully searching for any one of the strings on all lines of a file and replacing it with the instructed value. cat inputFile | awk '{gsub(/aaa|bbb|ccc|ddd/,"1234")}1' > outputFile This does in fact replace any occurrence of aaa, bbb,... (2 Replies)
Discussion started by: dazhoop
2 Replies

7. Shell Programming and Scripting

how to give multiple csv files as input in awk

Hi All, I am new to shell scripting..My problem is i want to give multiple csv files as input to awk script and process the data into one file.. My input file is File1 File2 File3 Product Location Period SalesPrice A x 8/11/2010 ... (7 Replies)
Discussion started by: kvth
7 Replies

8. Shell Programming and Scripting

redirect an awk string output to a script input with pipes

Hi, I have a function in a bash script that returns a string after some operations using awk. The following code returns 555 $VARIABLE="EXAMPLE" get_number $VARIABLE this value I'd like to pass it as a second argument of another script with the following usage myscript.sh <param1>... (7 Replies)
Discussion started by: rid
7 Replies

9. Shell Programming and Scripting

Multiple search string in multiple files using awk

Hi, filenames: contains name of list of files to search in. placelist contains the names of places to be searched in all files in "filenames" for i in $(<filenames) do egrep -f placelist $i if ] then echo $i fi done >> outputfile Output i am getting: (0 Replies)
Discussion started by: pinnacle
0 Replies

10. Shell Programming and Scripting

Multiple input field Separators in awk.

I saw a couple of posts here referencing how to handle more than one input field separator in awk. I figured I would share how I (just!) figured out how to turn this line in a logfile: 90000000000000000000010001 name... (4 Replies)
Discussion started by: kinksville
4 Replies
Login or Register to Ask a Question