Parsing via sed issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parsing via sed issue
# 1  
Old 09-27-2018
Parsing via sed issue

sorry I messed up the last post with too many mistakes and corrections so I closed it and opening a new one which should be clear to everyone .my apologies to the admins.


I am using sun solaris and Linux , what I want is SED to print any string (or output it to a file preferably) that does not have either "01","03","05","07","10" or "11" on the 9th and 10th position .
e.g from the file below I only want these three lines


Code:
433483433339
167710001710
167730600000


Code:
$cat a.txt
 000000001000
 433483433339 <<< print this since 33 is at 9th and 10th pos
 121121211100 
 167710001710 <<< print this since 17 is at 9th and 10th pos
 167735250310
 167735260510
 167735280710
 167730600000 <<< print this since 00 is at 9th and 10th pos


hope I am clear this time.
thanks

Moderator's Comments:
Mod Comment edit by bakunin: we have no problems at all with messed-up posts, but we do take issue with missing CODE-tags. I'd appreciate it if i don't have to edit them in for you in the future because you use them yourself.

Last edited by bakunin; 09-27-2018 at 02:22 AM..
# 2  
Old 09-27-2018
Quote:
Originally Posted by boncuk
I am using sun solaris and Linux , what I want is SED to print any string (or output it to a file preferably) that does not have either "01","03","05","07","10" or "11" on the 9th and 10th position
This is done with a simple rule. sed will per default print every line in the input file, so we just have to "delete" the lines we are not interested in.

Code:
sed '/^........0[1357]/d' /your/input/file

Notice that this does not address the lines with "10" and "11". We will get to this but first let us have a look at the regular expression:

Code:
/^........0[1357]/

It says: "^" is the beginning of line, so the beginning of line, followed by 8 characters ("." is "any character), followed by a "0". This is followed by a character class: "[1357]" means any one (but only one) character of the mentioned set. So in fact it is a convenient way of saying either "01" or "03" or "05" or "07".

The following list of commands - in this case only one command, the "d" - is executed and so these lines do not get printed, all others do.

We have left out two more types of lines you also wanted to exclude: "10" and "11" in 9th-10th position. It would be possible to put that all into one complex regex, but it is far easier (and, in my humble opinion, easier to read) to simply have two rules instead of one. Therefore:

Code:
sed -n '/^........0[1357]/d
        /^........1[01]/d' /your/input/file

Which should do what you want. Notice that sed does not change your input file so deleting lines only affects the output, not your source. If you want to lastingly change your inut you have to do it in two steps: let sed write to a different output file, then overwrite the source with this:

Code:
sed -n '/^........0[1357]/d
        /^........1[01]/d' /your/input/file > /some/output
mv /some/output /your/input/file


I hope this helps.

bakunin

Last edited by bakunin; 09-27-2018 at 03:04 AM..
# 3  
Old 09-27-2018
@bakunin: I'm afraid the -n option suppresses ANY printout regardless of lines being deleted (by the regexes found) or not. With the -n removed the solutions is close to the ones proposed in the requestor's other thread. Please continue discussions over there. This duplicate thread is closed.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue in awk parsing under while loop

Hi I am trying to parse a grep output using awk. It works fine individually and not working under the loop with variable name assigned. cat > file.txt dict=/dictr/abcd/d1/wq:/dictr/abcd/d2/wq:/dictr/abcd/d3/wq: sample tried code Nos=`grep -w "dict" file.txt | awk -F"=" '{print... (10 Replies)
Discussion started by: ananan
10 Replies

2. Shell Programming and Scripting

Issue with awk script parsing log file

Hello All, I am trying to parse a log file and i got this code from one of the good forum colleagues, However i realised later there is a problem with this awk script, being naive to awk world wanted to see if you guys can help me out. AWK script: awk '$1 ~ "^WRITER_" {p=1;next}... (18 Replies)
Discussion started by: Ariean
18 Replies

3. Shell Programming and Scripting

Parsing issue while reading excel having 3000 record

Hi Need urgent help (2 Replies)
Discussion started by: premp26
2 Replies

4. Shell Programming and Scripting

sed (parsing value)

All, Can somebody provide me with some sed expertise on how to parse the following line. 27-MAR-2011 10:28:01 * (CONNECT_DATA=(SID=dmart)(CID=(PROGRAM=sqlplus)(HOST=mtasnprod1)(USER=mtasnord))) * (ADDRESS=(PROTOCOL=tcp)(HOST=10.197.7.47)(PORT=54881)) * establish * dmart * 0 I would like... (3 Replies)
Discussion started by: BeefStu
3 Replies

5. Shell Programming and Scripting

Parsing cron with sed

Hello I want to convert my cron list into a csv Can you please help me with sed ? eg: Convert #06,21,36,51 * * 1,2 * (. ~/.profile ; timex /some/path/script -30 -15) >> /some/path/logfile2 2>&1 * * * * * (. ~/.profile ; timex /some/path/script2) > /some/path/logfile2 To:... (1 Reply)
Discussion started by: drbiloukos
1 Replies

6. Shell Programming and Scripting

Parsing issue

Scripting geeks please advice how this script should parse the input parameter to File Name convention to search the strings. Enclosed is the basic view of the search architecture. ##******************************************************************************************************* ## ... (2 Replies)
Discussion started by: raghunsi
2 Replies

7. UNIX for Dummies Questions & Answers

Issue with parsing config variables

I am using MKS tool kit on windows server. One config variable is defined in windows environment and I am trying to use that variable. # Below RootDir is defined in windows RootDir="\\f01\var" # in unix script details="$RootDir/src|$RootDir/tgt" src=`echo $details|awk -F '|' '{print... (1 Reply)
Discussion started by: madhukalyan
1 Replies

8. Shell Programming and Scripting

parsing issue with edi file

Hello, We have edi files we need to do some extra parsing on. There is a line that shows up that looks like this: GE|8,845|000000000 We need to parse the file, find the line ( that begins with GE "^GE" ), and remove the comma(s). What is the easiest way to do that ? I know I can grab... (5 Replies)
Discussion started by: fwellers
5 Replies

9. Shell Programming and Scripting

parsing logfiles (performance issue)

-------------------------------------------------------------------------------- Hi All, I am reading some logfiles and parsing data and printing to some textfile. Here is my code OLDIFS=$IFS IFS=' ' # just a newline, in single quotes while read data do if then #Parsing the... (4 Replies)
Discussion started by: subin_bala
4 Replies

10. Shell Programming and Scripting

awk sed parsing

hi , i would like to parse some file with the fallowing data : data data data "unwanted data" data data "unwanted data" data data data data #unwanted data. what i want it to have any coments between "" and after # to be erased using awk or/and sed. has anyone an idea? thanks. (3 Replies)
Discussion started by: Darsh
3 Replies
Login or Register to Ask a Question