Extract all the ocurrences in a String


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract all the ocurrences in a String
# 1  
Old 03-03-2009
Extract all the ocurrences in a String

Hi.

I'll do my best to write correctly ( I'm from Spain ).

I have to create a sh to read from a flat file. Each field is delimited by ":", so each line could be like this:

field1:field2:field3 and so on.

I will create a file which can read a flat file containing 1..n fields.

I've done all the process, reading all the lines. Each line I read, I'm invoking to extract the substring, having the pattern ":".

echo $a | awk '{FS=":"}{print $1}{print $2}'

Where $a is the current line in the file. Since I don't know how many fields per line that the file can have, I need some way to know how many delimiters have the line.

print $1 prints all the text up to the delimiter.
print $2 same, until the second delimiter and so on...

There's some parameter that tells me how many delimiters are? ( Just like $@ or $* tells the number or parameters to a function, in example )

Thanks in advance.

Regards.

pd: Sorry for the english :P
# 2  
Old 03-03-2009
I'm almost sure you want awk

Code:
awk -F: '{
              print "this line has ", NF, " columns: " $0
              print "these are the fields"
              for(i=1; i<=NF; i++) {
                    print $1
              }

           }' inputfilename

# 3  
Old 03-03-2009
Jim,

Thanks for your quick reply.

The code you wrote, works fine, this is exactly what I wanted.

Just a note. where you put print $1, must be print $i

Thanks again Smilie
# 4  
Old 03-03-2009
yes - my bad it should be $i.
# 5  
Old 03-03-2009
Just curiosity....

Is there any way to do this without calling awk?

I tried something like:

((i=0))
while [[ $a = *:* ]] ; do
....
((i=i+1))
done

But I'm still a newbie...
# 6  
Old 03-03-2009
Code:
#!/bin/ksh

file=inputFile

while IFS=':' read a
do
   set -- $a

   printf "this line has %d columns: %s\n" "$#" "$a"

   cnt=1
   print "these are the fields"
   while [ $cnt -le $# ]
   do
      eval print \$$cnt
      cnt=$(( $cnt + 1 ))
   done
done < ${file}


Last edited by vgersh99; 03-03-2009 at 10:56 AM.. Reason: spelling
# 7  
Old 03-03-2009
vgersh99,

Thanks for the code. It works prefectly.

jim mcnamara, vgersh99, thanks again. I've learned a lot today with your tips & codes.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to extract every repeated string between two specific string?

Hello guys, I have problem with hpux shell script. I have one big text file that contains like SOH bla bla bla bla bla bla ETX SOH bla bla bla ETX SOH bla bla bla ETX What I need to do is save first SOH*BLA into file1.txt, save second SOH*BLA into file2.txt and so on.... (17 Replies)
Discussion started by: sembii
17 Replies

2. Shell Programming and Scripting

To Search for a string and to extract the string from the text

Hi Team I have an huge xml where i need to search for a ceratin numbers. For example 2014-05-06 15:15:41,498 INFO WebContainer : 10 CommonServicesLogs - CleansingTriggerService.invokeCleansingService Entered PUBSUB NOTIFY MESSAGE () - <?xml version="1.0" encoding="UTF-8"... (5 Replies)
Discussion started by: Kannannair
5 Replies

3. Shell Programming and Scripting

Search String and extract few lines under the searched string

Need Assistance in shell programming... I have a huge file which has multiple stations and i wanted to search particular station and extract few lines from it and the rest is not needed Bold letters are the stations . The whole file has multiple stations . Below example i wanted to search... (4 Replies)
Discussion started by: ajayram_arya
4 Replies

4. Shell Programming and Scripting

Extract a string from another string in UNIX

I have a string string="Please have a nice day and sleep well Replace_12123_31233_32134_12342 Good day" How do i replace "Replace_12123_31233_32134_1234" in the above string.?? Please help. Regards, Qwerty (3 Replies)
Discussion started by: qwertyu
3 Replies

5. Shell Programming and Scripting

Extract a string between 2 ref string from a file

Hi, May i ask if someone share some command for extracting a string between 2 ref string in a txt file My objective: i had a file with multiple lines and wants only to extract the string "watch?v=IbkAXOmEHpY" or "watch?v=<11 random character>", when i used "grep 'watch?=*' i got a results per... (4 Replies)
Discussion started by: jao_madn
4 Replies

6. Shell Programming and Scripting

to extract string from main string and string comparison

continuing from my previous post, whose link is given below as a reference https://www.unix.com/shell-programming-scripting/171076-shell-scripting.html#post302573569 consider there is create table commands in a file for eg: CREATE TABLE `Blahblahblah` ( `id` int(11) NOT NULL... (2 Replies)
Discussion started by: vivek d r
2 Replies

7. Shell Programming and Scripting

extract a string within a string using a pattern

hi all, i have a file name using the following pattern: PREFIX: AR SOURCE: LEGACY DATETIME: YYYYMMDD_HH24MISS SUFFIX: .txt sample filename: AR_LEGACY_20101104_105500.txt i want to extract the source which is LEGACY in this case. how do i do this using shell? thanks. (4 Replies)
Discussion started by: adshocker
4 Replies

8. Shell Programming and Scripting

Search for string in a file and extract another string to a variable

Hi, guys. I have one question: I need to search for a string in a file, and then extract another string from the file and assign it to a variable. For example: the contents of the file (group) is below: ... ftp:x:23: mail:x:34 ... testing:x:2001 sales:x:2002 development:x:2003 ...... (6 Replies)
Discussion started by: daikeyang
6 Replies

9. Shell Programming and Scripting

extract a string from main string

i need a shell program,to extract a string from a main string. main string is "madhu" sub string is "mad"..means any word from the main string please di this (1 Reply)
Discussion started by: madhu.it
1 Replies

10. UNIX for Dummies Questions & Answers

How to extract a portion of a string from the whole string

How to extract a portion of a string from a full string using unix. For example: Say source string is = "req92374923.log" I want only the numeric portion of the string say "92374923" how to do that in Unix. (2 Replies)
Discussion started by: ds_sastry
2 Replies
Login or Register to Ask a Question