sed question for substring search


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed question for substring search
# 1  
Old 04-26-2018
sed question for substring search

i have this data where i am looking for a two digit number 01,03,05 or 07.
if not found i should detect that .

this sed command gives me the matching rows . I want the opposite , i want the rows if the match is NOT found .
also the sed command is only looking for 01, can i add 03, 05, 07 to one command ?

Code:
-bash-4.4$ sed -n -e 's/^[0-9]\{8\}01/&/p'  a.a
000008050110010201NNN
000008060110010201NNN
000008080110010201NNN
000008090110010201NNN
000008100110010201NNN
000008110110010201NNN
000008120110010201NNN
000008130110010201NNN


Last edited by vgersh99; 04-26-2018 at 12:05 PM.. Reason: code tags, please!
# 2  
Old 04-26-2018
something along these lines:
Code:
sed -e '/^[0-9]\{8\}0[357]/d'

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 04-26-2018
its giving me all the records ?
e.g in the data below i only want the highlighted record since its 9th and 10th position DO NOT contain either 01,03,05,07

Code:
-bash-4.4$ more a.a
000008050110010201NNN
000008060310010201NNN
000008070610010201NNN
000008080110010201NNN
000008090110010201NNN
000008100110010201NNN
000008110110010201NNN
000008120110010201NNN
000008130110010201NNN

Code:
-bash-4.4$
-bash-4.4$ sed -e '/^[0-9]\{8\}0[357]/d' a.a
000008050110010201NNN
000008070610010201NNN
000008080110010201NNN
000008090110010201NNN
000008100110010201NNN
000008110110010201NNN
000008120110010201NNN
000008130110010201NNN


Last edited by vgersh99; 04-26-2018 at 12:23 PM.. Reason: once again, please start using code tags!
# 4  
Old 04-26-2018
sorry forgot 01:
Code:
sed -e '/^[0-9]\{8\}0[1357]/d'

This User Gave Thanks to vgersh99 For This Post:
# 5  
Old 04-26-2018
btw is it possible to just end the sed command even if one nomatching record is found ? i have a large file to search these records and its taking long time to come out .
# 6  
Old 04-26-2018
Quote:
Originally Posted by boncuk
btw is it possible to just end the sed command even if one nomatching record is found ? i have a large file to search these records and its taking long time to come out .
how about awk:
Code:
awk '!/^[0-9]\{8\}0[1357]/{print;exit}' a.a

# 7  
Old 04-26-2018
Quote:
Originally Posted by boncuk
btw is it possible to just end the sed command even if one nomatching record is found ? i have a large file to search these records and its taking long time to come out .
You can use a so-called rule for this kind of tasks:

Code:
/<regexp>/ {
           command1
           command2
           command3
           ....
}

will execute command1, command2 and command3 only for lines which fit the regexp. Think of it like a "if..then..else"-construct in other languages. You can also invert it by adding an exclamation mark in front of the brackets:

Code:
/<regexp>/ ! {
           command1
           command2
           command3
           ....
}

Same as above but only for lines which do NOT fit the regexp.

This way you can use the sed-command q to leave immediately when a certain line is found, Example:

Code:
/^--TheEnd--/ {
          q
}

Will quit sed (and hence ignore all following lines) after the first line starting with the string "--TheEnd--" is found.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Another sed/awk search=>replace question

Hello, Need a little bit of help. Basically I need to replace lines in a file which were calculated wrong as it would 12 hours to regenerate the data. I need to calculate values based on other files which I've managed to figure out with grep/cut but now am stuck on how to shove these new... (21 Replies)
Discussion started by: f77coder
21 Replies

2. Shell Programming and Scripting

Search substring in a column of file

Hi all, I have 2 files, the first one containing a list of ids and the second one is a master file. I want to search each id from the first file from the 5th col in the second file. The 5th column in master file has values separated by ';', if not a single value is present. Each id must occur... (2 Replies)
Discussion started by: ritakadm
2 Replies

3. Shell Programming and Scripting

To Search for a pattern and substring text in a file

I have the following data in a text file. "A",1,"MyTextfile.CSV","200","This is ,line one" "B","EFG",23,"MyTextfile1.csv","5621",562,"This is ,line two" I want to extract the fileNames MyTextfile.CSV and MyTextfile1.csv. The problem is not all the lines are delimited with "," There are... (3 Replies)
Discussion started by: AshTrak
3 Replies

4. Shell Programming and Scripting

AWK: Substring search

Hi I have a table like this I want to know how many times the string in 2nd column appears in the first column as substring. For example the first string of 2nd column "cgt" occurs 3 times in the 1st column and "acg" one time. So my desired output is THank you very much in advance:) (14 Replies)
Discussion started by: polsum
14 Replies

5. Shell Programming and Scripting

Bash sed search and replace question

I have several files that I need to modify using sed. I know how to do that, but now a new requirement has come up. Now, I need to make changes to all lines that don't start with certain strings. For example, I need to change all lines except for lines that start with "a", "hello there",... (3 Replies)
Discussion started by: RickS
3 Replies

6. Shell Programming and Scripting

SED Question: Search and Replace start of line to matching pattern

Hi guys, got a problem here with sed on the command line. If i have a string as below: online xx:wer:xcv: sdf:/asdf/http:https-asdfd How can i match the pattern "http:" and replace the start of the string to the pattern with null? I tried the following but it doesn't work: ... (3 Replies)
Discussion started by: DrivesMeCrazy
3 Replies

7. UNIX for Dummies Questions & Answers

search for string and return substring

Hi, I have a file with the following contents: I need to create a script or search command that will search for this string 'ENDC' in the file. This string is unique and only occurs in one record. Once it finds the string, I would like it to return positions 101-109 ( this is the date of... (0 Replies)
Discussion started by: Lenora2009
0 Replies

8. Shell Programming and Scripting

Using sed to get a substring

Hi, I have looked all over for this. I am attempting to get a the substring of a string using sed since it seemed the best solution for this. For example my string is: "zzz foo to you and bar123 or foo" I would like to extract the text between "and" and "or" (it could be anything, but... (2 Replies)
Discussion started by: CentaurAtlas
2 Replies

9. UNIX for Dummies Questions & Answers

grep exact string/ avoid substring search

Hi All, I have 2 programs running by the following names: a_testloop.sh testloop.sh I read these programs names from a file and store each of them into a variable called $program. On the completion of the above programs i should send an email. When i use grep with ps to see if any of... (3 Replies)
Discussion started by: albertashish
3 Replies

10. Shell Programming and Scripting

SED Search and Replace Question for Google Analytics

Well, I'm losing my regex ability in sed! Please help. I need to search for this text in multiple html files in a directory: </body> and add the following lines in front of the text above: <script src="http://www.google-analytics.com/urchin.js" type="text/javascript"> </script> ... (11 Replies)
Discussion started by: Neo
11 Replies
Login or Register to Ask a Question