LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 01-27-2016, 06:03 PM   #1
ntbluez
LQ Newbie
 
Registered: Nov 2015
Posts: 21

Rep: Reputation: Disabled
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

Last edited by ntbluez; 01-27-2016 at 08:42 PM.
 
Old 01-27-2016, 06:11 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,119

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Show us what you did, and the output of each command.
 
Old 01-27-2016, 08:12 PM   #3
ntbluez
LQ Newbie
 
Registered: Nov 2015
Posts: 21

Original Poster
Rep: Reputation: Disabled
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'

Last edited by ntbluez; 01-27-2016 at 08:41 PM.
 
Old 01-27-2016, 08:50 PM   #4
ntbluez
LQ Newbie
 
Registered: Nov 2015
Posts: 21

Original Poster
Rep: Reputation: Disabled
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
 
Old 01-27-2016, 09:46 PM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,119

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Not surprising. Check the output at each stage and see what's happening at each pipe.
 
Old 01-28-2016, 07:44 AM   #6
fmattheus
Member
 
Registered: Nov 2015
Posts: 104

Rep: Reputation: 38
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.
 
Old 01-28-2016, 07:53 AM   #7
ntbluez
LQ Newbie
 
Registered: Nov 2015
Posts: 21

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

example -c -i567 -i8a9b"
example -c -g123 -g456"
 
Old 01-28-2016, 07:55 AM   #8
fmattheus
Member
 
Registered: Nov 2015
Posts: 104

Rep: Reputation: 38
Very good. Now instead of '^', as I understand it, you want to start with 'Stop'
 
Old 01-28-2016, 08:59 AM   #9
ntbluez
LQ Newbie
 
Registered: Nov 2015
Posts: 21

Original Poster
Rep: Reputation: Disabled
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!
 
Old 01-29-2016, 01:48 AM   #10
fmattheus
Member
 
Registered: Nov 2015
Posts: 104

Rep: Reputation: 38
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|'
 
1 members found this post helpful.
Old 01-30-2016, 09:50 AM   #11
ntbluez
LQ Newbie
 
Registered: Nov 2015
Posts: 21

Original Poster
Rep: Reputation: Disabled
@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!
 
Old 01-30-2016, 12:14 PM   #12
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,776

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
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.

Last edited by rknichols; 01-30-2016 at 12:17 PM.
 
1 members found this post helpful.
Old 01-30-2016, 12:44 PM   #13
ntbluez
LQ Newbie
 
Registered: Nov 2015
Posts: 21

Original Poster
Rep: Reputation: Disabled
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
 
Old 01-30-2016, 02:02 PM   #14
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,776

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by ntbluez View Post
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%'
 
1 members found this post helpful.
Old 01-30-2016, 07:08 PM   #15
ntbluez
LQ Newbie
 
Registered: Nov 2015
Posts: 21

Original Poster
Rep: Reputation: Disabled
Awesome feedback! Truly appreciate all your help!!!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
cat|grep|awk pudhiyavan Programming 6 07-14-2008 01:56 AM
cat and grep dipuasks Linux - General 11 04-21-2008 03:39 AM
cat to grep to textfile? jabfinger Linux - General 3 09-05-2006 03:25 PM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
Question About Cat And Grep George2 Linux - Software 10 03-09-2006 03:33 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 06:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration