Need Help with sed

Hello Folks,

I use this sed line to search and print a specific string in multiple lines with a bunch of strings on each:

sed -n '/[A-Z][0-9]\{5,\}/s/^.*\([A-Z][0-9]\{5,\}\).*$/\1/p' job_details.txt

-> Output Sample:

B08619

However, I'm having a hard time printing multiple patterns from a line.

Cat job_details.txt

86,blah blah blah, B08619,drive index 10, blah blah blah...

Desired Output: (including comma)

86, B08619, drive index 10

Please help!

Thanks in advance.
59626

I don't understand what you're trying to do.
Your current input sample contains one occurrence of an upper case letter followed by five or more decimal digits and discards everything before and after that pattern. It doesn't attempt to treat commas as special.

Your input file has five comma separated fields. Your output discards the 2nd and 5th fields, inserts a space in the 4th field that wasn't present in the input, and preserves a space in the 3rd field that your sed command seems to want to discard.

Please CLEARLY explain what you are trying to do and be sure that the sample output you provide matches the sample input you provide when the transformation rules you supply are applied.

1 Like

Hi Don, thanks for editing my post and the reminders. I read the instructions before I posted but didn't realize I hit <> - for HMTL tags instead of the code button. Anyway, what I was trying to accomplish is search for multiple patterns in a line and print them out with comma as a separator, using sed command.

86,this,is,the,best,forum,for,newbies,B08619
86,2nd,line,B08620
86,missing,piece,of,my,backup,script,B08621

How can I edit my sed command to search the lines above to get the following output: (getting rid of all characters but the patterns and then add a comma)

86,B08619
86,B08620
86,B08621

If you only want to retain certain field on a coma separated file you can do it several ways.
The last sed statement below assume you exactly have 5 fields (coma separated)
Some ways to do it ...

$ cat tst
86,blah blah blah, B08619,drive index 10, blah blah blah
$ cut -d, -f1,3,4 tst
86, B08619,drive index 10
$ awk -F, '{print $1,$3,$4}' OFS=, tst
86, B08619,drive index 10
$ sed 's/\([^,]*,\)[^,]*,\([^,]*,[^,]*\),.*$/\1\2/' tst
86, B08619,drive index 10
$ sed 's/,[^,]*//;s/,[^,]*$//' tst
86, B08619,drive index 10

Include what you want to retain in \( \) and perform back reference to it when reconstructing the output with the \1 \2 ....
example :

$ cat tst
86,this,is,the,best,forum,for,newbies,B08619
86,2nd,line,B08620
86,missing,piece,of,my,backup,script,B08621
$ sed 's/\(86\).*\(B[0-9]*\)$/\1,\2/' tst
86,B08619
86,B08620
86,B08621
$ sed 's/\(86\).*\(B[0-9]*\)$/\2,\1/' tst
B08619,86
B08620,86
B08621,86
$

Giving real data or as close to real data does help us lot of helping your.
Your example in post #3, can easily be solved like this.
And sed are not always the best tool for all job :slight_smile:

awk -F, '{print $1 FS $NF}' file
86,B08619
86,B08620
86,B08621

Print the first and last field.

You still haven't explained what patterns you're trying to preserve. With the above sample input and output, the following command (which completely ignores any pattern that might match the text that will be preserved) does what you show that you want:

sed 's/,.*,/,/' job_details.txt