Find a string occurrence if twice in a line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find a string occurrence if twice in a line
# 1  
Old 07-15-2013
Find a string occurrence if twice in a line

Hello All,

I want to check if a delimiter is existing twice in a line of a text file.

Suppose flat file is like this

Code:
234 | 123
123 | 345
456 | 563 |
234 | 548

So the the 3rd line has two delimiters,
How can we find the lines in such a file having more then one delimiters

I tried with

Code:
cat sample.txt  |  grep -c '|'

but this gives me the count of total number of delimiters.

Any help appreciated
# 2  
Old 07-15-2013
You can use below code.

Code:
 
awk '/.*\|.*\|/ {print}' sample.txt

# 3  
Old 07-15-2013
Code:
awk -F\| 'NF > 2' file

# 4  
Old 07-15-2013
Code:
awk -F"|" '{print "Line "NR" Contains "NF-1" | Character(s)"}' file

Line 1 Contains 1 | Character(s)
Line 2 Contains 1 | Character(s)
Line 3 Contains 2 | Character(s)
Line 4 Contains 1 | Character(s)

# 5  
Old 07-15-2013
That works good,

Thanks a lot.

How can I count number of pipes '|' on each line in my text file.
# 6  
Old 07-15-2013
To get count of pipes:
Code:
awk '{n=gsub(/\|/,"&"); print n}' file

To print records with more than one pipe:
Code:
awk '{n=gsub(/\|/,"&"); if (n > 1) print $0}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Replace consecutive occurrence of string in same line

Hi All, I have a requirement to replace consecutive occurence of same string nedd to be replaced. Below is the input and desired output. Input: --------- 123.5|ABC|.|.|. 234.4|DEF|.|.|.|.|.| Output: --------- 123.5|ABC|||. 234.4|DEF||||| so basically "|.|" need to be replaced with... (9 Replies)
Discussion started by: ureddy
9 Replies

2. Shell Programming and Scripting

Find a string and its position in a line from another string

Hello guys, would you please help me with this? this is the line inside a file: first line Something Today YYDDPPSVXIPYYY0XXXOFFS00000000000? I'd like to find the position of string XXX from string PYYY In the example above XXX starts from 6th position from PYYY desired... (4 Replies)
Discussion started by: netrom
4 Replies

3. UNIX for Advanced & Expert Users

How to find a string in a line in UNIX file and delete that line and previous 3 lines ?

Hi , i have a file with data as below.This is same file. But actual file contains to many rows. i want to search for a string "Field 039 00" and delete that line and previous 3 lines in that file.. Can some body suggested me how can i do using either sed or awk command ? Field 004... (7 Replies)
Discussion started by: vadlamudy
7 Replies

4. Shell Programming and Scripting

Find last occurrence of a character in a string

Hello how to find last occurence of a string for example in the following I want last occurence of '-' i.e. position 12 str="aa-bbb-cccc-ddd-ee" my pupose is to get the string 'ee' Thanks and Regards Chetanz (5 Replies)
Discussion started by: Chetanz
5 Replies

5. Emergency UNIX and Linux Support

Find a line using a condition and replace a string in that line

Hello, I have a 100 line code. I have given a sample of it below: ABC*654654*1*54.54*21.2*87*1*654654654654 CCC*FS*FS*SFD*DSF GGG*FGH*CGB*FBDFG*FGDG ABC*654654*1*57.84*45.4*88*2*6546546545 CCC*WSF*SG*FGH*GHJ ADA*AF*SFG*DFGH*FGH*FGTH I need to select the line starting with "ABC" its... (3 Replies)
Discussion started by: nithins007
3 Replies

6. Shell Programming and Scripting

Find a line using a condition and replace a string in that line

Hello, I have a 100 line code. I have given a sample of it below: ABC*654654*1*54.54*21.2*87*1*654654654654 CCC*FS*FS*SFD*DSF GGG*FGH*CGB*FBDFG*FGDG ABC*654654*1*57.84*45.4*88*2*6546546545 CCC*WSF*SG*FGH*GHJ ADA*AF*SFG*DFGH*FGH*FGTH I need to select the line starting with "ABC" its... (6 Replies)
Discussion started by: nithins007
6 Replies

7. Shell Programming and Scripting

find string nth occurrence in file and print line number

Hi I have requirement to find nth occurrence in a file and capture data from with in lines (between lines) Data in File. <QUOTE> <SESSION> <ATTRIBUTE NAME='Parameter Filename' VALUE='file1.parm'/> <ATTRIBUTE NAME='Service Name' VALUE='None'/> </SESSION> <SESSION> <ATTRIBUTE... (6 Replies)
Discussion started by: tmalik79
6 Replies

8. Shell Programming and Scripting

remove characters from string based on occurrence of a string

Hello Folks.. I need your help .. here the example of my problem..i know its easy..i don't all the commands in unix to do this especiallly sed...here my string.. dwc2_dfg_ajja_dfhhj_vw_dec2_dfgh_dwq desired output is.. dwc2_dfg_ajja_dfhhj it's a simple task with tail... (5 Replies)
Discussion started by: victor369
5 Replies

9. Programming

Find a line containing a string.

Hello. I have a large file that contains a lot of gibberish and also a lot of http addresses. How can i read the file, take out the http addresses, and write each one of them on one line each into another file? It looks something like this. ... (1 Reply)
Discussion started by: cbreiny
1 Replies

10. Shell Programming and Scripting

find the string in a line

Hello I have a abc.txt file which semicolon delimited. I need to read the first line of the abc.txt file where i need to extract specific string which at specific location. i.e first line of the abc.txt file is as below abc;123;xyz;345;678 my requirement is i need to get the 678. I was... (7 Replies)
Discussion started by: dsdev_123
7 Replies
Login or Register to Ask a Question