LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   Help using GREP SED CAT (https://www.linuxquestions.org/questions/linux-server-73/help-using-grep-sed-cat-4175568081/)

ntbluez 01-27-2016 06:03 PM

Help using GREP SED CAT
 
I have a file (example.txt) which includes the data strings. From linux command line,

requirements:
(a) I want the output to replace letters SS with the word, example.
(b) I also need to remove all garbage from "Stop" all way up to the word example, but do not delete the word example.
Here is something like what I am attempting to do.

Original File (example.txt)
1 "Stop managed application instances (xyz1 xyz2 xyz3) toejam.help.com (abc1 abc2 abc3) tojam1.help.com SS -c -gXYZA -gXYZB"
20 "Stop managed application instances (efg4 efg5 efg6) toejam.help.com (efg7 efg8 efg9) toejam2.help.com SS -c -gEFGA -gEFGB"

New File (should look like this)
1 /home/joe/script/example -c -gXYZA -gXYZB
20 /home/joe/script/example -c -gEFGA -gEFGB

I've tried using both grep and sed but not able to produce the desired results.
I know I can easily replace SS with any word of my choice, like "example" for instance...
Will appreciate your help!

NTBluez

syg00 01-27-2016 06:11 PM

Show us what you did, and the output of each command.

ntbluez 01-27-2016 08:12 PM

cat example.txt | cut -c 1-3 | sed s/^.* *.com//g | sed -i -- 's/SS/example/g' *

cut -c 1-3 is to get the numbers of each line, like 1 and 20, etc... each line has a number, but is not a line number...

I also tried >
cat example.txt | sed 's/|//' | awk '{print $1}' | sed 's/*.com//g'

ntbluez 01-27-2016 08:50 PM

Simple example of what I'm trying to do...

File Name: example.txt has 2 lines, those are not line numbers; 10 and 20.

10 "Stop abcdee fghi jk lmnz toejamz.com SS -c -i567 -i8a9b"
20 "Stop pqrstu vwxyz toejamz2.com SS -c -g123 -g456

I want to output a new file which does not have "Stop.... to tojamz.com"
New file look like this >
10 example -c -i567 -i8a9b
20 example-c -g123 -g456

Does this help you, so you can help me create the new output file?
cat example.txt | sed 's/|//' | awk '{print $1}' | sed 's/*.com//g' >> example2.txt <---- does not work for me

syg00 01-27-2016 09:46 PM

Not surprising. Check the output at each stage and see what's happening at each pipe.

fmattheus 01-28-2016 07:44 AM

As syg00 said, when working on piped commandlines try out each pipe one at a time to see if they did what you expected before adding another
Code:

cat example | sed 's/|//'
10 "Stop abcdee fghi jk lmnz toejamz.com SS -c -i567 -i8a9b"
20 "Stop pqrstu vwxyz toejamz2.com SS -c -g123 -g456

This one did nothing. Not sure what you expected

Code:

cat example | awk '{print $1}'
10
20

This one printed out the first column, now you've lost all other information. DOH!

Code:

cat example | sed 's/*.com//g'
10 "Stop abcdee fghi jk lmnz toejamz.com SS -c -i567 -i8a9b"
20 "Stop pqrstu vwxyz toejamz2.com SS -c -g123 -g456

This one also did nothing, but it had the most potential. The '*' character doesn't do what you thuink it does. Look into regexes and find out what does. Once you've figured it out, try the following command, replacing * with what it should be.

Code:

cat example | sed 's/Stop*SS/example/g'
10 "example -c -i567 -i8a9b"
20 "example -c -g123 -g456

P.S. - you also don't need the 'g' at the end, as your regex only occurs once per line.

ntbluez 01-28-2016 07:53 AM

Making some progress here with > $ cat test.txt | sed 's/^.* SS/example/' whose output is:

example -c -i567 -i8a9b"
example -c -g123 -g456"

fmattheus 01-28-2016 07:55 AM

Very good. Now instead of '^', as I understand it, you want to start with 'Stop'

ntbluez 01-28-2016 08:59 AM

Another problem, when using the sed 's/something/here/' what if I want the "here" to be a directory. Example, sed 's/Stop.* SS/(/home/joe/test)/' does not work. I am attempting to replace with a directory name & its not working...
Thanks!

fmattheus 01-29-2016 01:48 AM

You have set / as the delimiter. So sed gets confused when you put all those extra slashes in there. You either need to escape the "directory" slashes with a backslash, eg -
Code:

sed 's/Stop.* SS/\/home\/joe\/test/'
Or use a different delimiter, like this
Code:

sed 's|Stop.* SS|/home/joe/test|'

ntbluez 01-30-2016 09:50 AM

@FMATTHEUS,

File (A) contains a line like this...
660 Advisor RAA "Start RAA Pod WS/ChordiantEJB (Cluster 3) Managed Instances server1raa01.toejamz.com EFGH/SuperScriptSS -gRAA"

And I want the output to look like this...
660 /home/joe/script/app -gRAA

Note: Where 660 is not a line number
I tried to no avail using sed, grep and cat commands & have at least 12 hours in to figuring this out (wasting time).

Can I ask of you to simply provide the solution to save my sanity??? PLEASE. My wife would appreciate it as I am wasting time trying to figure this out and not paying enough attention to her. Thank you!

rknichols 01-30-2016 12:14 PM

It's a bit complex, but fairly straightforward. If that first field is always numeric and the last is always alphabetic, then:
Code:

sed -r 's%([0-9]+).* (-[[:alpha:]]+)"%\1 /home/joe/script/app \2%'
Explanation:
Code:

-r              Use extended regular expressions (saves needing a lot of backslashes)
s%              Substitute, using "%" as the delimiter character
([0-9]+)        First subexpression matches one or more digits 0-9
.*              Matches anything, plus a required space
(-[[:alpha:]]+) Second subexpression matches a "-" sign followed by one or more alphabetic characters
"              Matches the terminal quote
%              Delimiter separating the regex from the replacement string
\1              Insert the characters that matched the first parenthesized subexpression
/home/joe/...  Literal text
\2              Insert the characters that matched the second parenthesized subexpression
%              Final delimiter

Note, this forum software can eat "%" signs. Watch out for that when replying, and use the "Advanced" editor to avoid the problem.

ntbluez 01-30-2016 12:44 PM

Hmm. I am getting illegal -r when using sed in your example. My last word is "help" as it is the filename I was attempting to apply your command on. My help file contains the string in the example I provided which you commented on > 660 Advisor RAA "Start RAA Pod WS/ChordiantEJB (Cluster 3) Managed Instances server1raa01.toejamz.com EFGH/SuperScriptSS -gRAA"

sed -r 's([0-9]+).* (-[[:alpha:]])"&\1 /home/joe/script/app \2%' help
sed: illegal option -- r

rknichols 01-30-2016 02:02 PM

Quote:

Originally Posted by ntbluez (Post 5490411)
sed -r 's([0-9]+).* (-[[:alpha:]])"&\1 /home/joe/script/app \2%' help
sed: illegal option -- r

There are multiple typos in that command line, but none of them relate to the problem with the "-r" option. You are missing the "%" immediately following the "s" (probably you had it in there but ran into the issue with the forum software), you are missing the "+" following "[[:alpha:]]", and that "&" in the middle of the line should be "%".

What is the output from "sed --version" ? You seem to have a version that does not support extended regular expressions.

Anyway, here is the same command but with a basic regular expression:
Code:

sed 's%\([0-9]\+\).* \(-[[:alpha:]]\+\)"%\1 /home/joe/script/app \2%'

ntbluez 01-30-2016 07:08 PM

Awesome feedback! Truly appreciate all your help!!!


All times are GMT -5. The time now is 09:51 AM.