awk for trimming a string up to the first, then second, then third... match


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers awk for trimming a string up to the first, then second, then third... match
# 1  
Old 12-08-2012
awk for trimming a string up to the first, then second, then third... match

Hi !

With awk, I would need to trim a string from the beginning up to the first occurrence of "1", then from the beginning up to the second occurrence of "1", then from the beginning up to the third, then the fourth...., then the last occurrence of "1".

input:
Code:
1aaa1bb1ccccccc
dd1e1ffff
...

expected output:
Code:
1aaa1bb1ccccccc
1bb1ccccccc
1ccccccc
1e1ffff
1ffff

I am trying with something like that but it works only for the first occurrence:
Code:
gawk '{a = match($0,1); print substr($0,a)}' input

Is there a way with match function (or other) to returns all the positions of the matches and not only the longest leftmost match ?

Thanks for your help !
# 2  
Old 12-08-2012
Try:
Code:
awk -F1 -v OFS=1 '{
        while(NF > 1) {
                $1=""
                print
                $0=substr($0, 2)
        }
}' input

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 12-09-2012
Awesome !

Thanks Don Cragun !
# 4  
Old 12-09-2012
Another approach:-
Code:
awk ' {
 for(i=1;i<=length;i++)
 {
  if(substr($0,i,1)==1)
   print substr($0,i,length)
 }
} ' infile

# 5  
Old 12-09-2012
Thanks for your help bipinajith !
I like this one too.

I'm just curious. It works with "1", but surprisingly (at least for me...) it doesn't work with letters.

If I replace "1" by "X" for instance:
Code:
XaaaXbbXccccccc

it returns a blank line, when I modify your code to:
Code:
awk '{
	for(i=1;i<=length;i++){
		if(substr($0,i,1)==X){
		print substr($0,i,length)
		}
		}
}'

# 6  
Old 12-09-2012
Put letters and strings in double quotes:-
Code:
if(substr($0,i,1)=="X"){

This User Gave Thanks to Yoda For This Post:
# 7  
Old 12-09-2012
sorry...Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk, if line after string does not match insert

I have a large file with interface records. I need to check every record that has the string "encapsulation bridge1483" and if the next line after this does not have "ip description" then I need to insert a line to add "ip description blah_blah_blah. Sample file: interface atm 1/0.190158... (3 Replies)
Discussion started by: numele
3 Replies

2. Shell Programming and Scripting

Unable to match string within awk printf

Hi All I am working to process txt file into csv commo separated. Input.txt 1,2,asdf,34sdsd,120,haahha2 2,2,wewedf,45sdsd,130,haahha ..... .... Errorcode.txt 120 130 140 myawk.awk code: { BEGIN{ HEADER="f1,f2,f3,f4,f5,f6" (4 Replies)
Discussion started by: krsnadasa
4 Replies

3. Shell Programming and Scripting

awk : match the string and string with the quotes :

Hi all, Here is the data file: - want to match only lan3 in the output . - not lan3:1 file : OPERATING_SYSTEM=HP-UX LOOPBACK_ADDRESS=127.0.0.1 INTERFACE_NAME="lan3" IP_ADDRESS="10.53.52.241" SUBNET_MASK="255.255.255.192" BROADCAST_ADDRESS="" INTERFACE_STATE=""... (2 Replies)
Discussion started by: rveri
2 Replies

4. Shell Programming and Scripting

Trimming a string

Hi I need to trim white spaces from strings in a file. Input file is like this: 1_rrc_CatalogGroups.csv = 607 1_rrc_Sales_TopCatalogGroups.csv = 4 1_rrc_Sales_CatalogEntries_CatalogGroup_Rel.csv = 7 Need to trim space before and after = symbol. This is my script: #!/usr/bin/ksh ... (2 Replies)
Discussion started by: sukhdip
2 Replies

5. UNIX for Dummies Questions & Answers

Trimming a string based on delimiter.

Hi, I have a string say "whateverCluster". I need everthing apart from the string "Cluster" Input: whateverCluster Desired output: whatever (5 Replies)
Discussion started by: mohtashims
5 Replies

6. Shell Programming and Scripting

match string exactly with awk/sed

Hi all, I have a list that I would like to parse with awk/sed. The list is contains entries such as: JournalTitle: Biochemistry JournalTitle: Biochemistry and cell biology = Biochimie et biologie cellulaire JournalTitle: Biochemistry and experimental biology JournalTitle: Biochemistry and... (6 Replies)
Discussion started by: euval
6 Replies

7. Shell Programming and Scripting

How to get part of string in awk from match

Hi, Im an awk noob and I am having trouble trying to get matches. Here is my script: #!/bin/gawk -f BEGIN {} $0 ~ /<a href=".*">.*<\/a>/{print} Ideally I want to be able to get the actual link and print it. In PHP you can do preg_replace and get the match you want by using \\1 where 1... (2 Replies)
Discussion started by: adsyuk
2 Replies

8. Shell Programming and Scripting

How can I match lines with just one occurance of a string in awk?

Hi, I'm trying to match records using awk which contain only one occurance of my string, I know how to match one or more (+) but matching only one is eluding me without developing some convoluted bit of code. I was hoping there would be some simple pattern matching thing similar to '+' but... (9 Replies)
Discussion started by: jonathanm
9 Replies

9. UNIX for Advanced & Expert Users

how can awk match multi pattern in a string

Hi all, I need to category the processes in my system with awk. And for now, there are several command with similar name, so i have to match more than one pattern to pick it out. for instance: binrundb the string1, 2 & 3 may contain word, number, blank or "/". The "bin" should be ahead "rundb"... (5 Replies)
Discussion started by: sleepy_11
5 Replies

10. Shell Programming and Scripting

Trimming a string

Hi, I am trying to find a script command that will let me trim leading and trailing space from a string. I have coded a SQL Select and sending the output to a file. Later I am parsing the file and reading each field. The problem is that each field uses the same size as the DB2 type it was defined... (2 Replies)
Discussion started by: fastgoon
2 Replies
Login or Register to Ask a Question