Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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!
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
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.
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!
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 -
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!
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.
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
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%'
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.