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 |
Show us what you did, and the output of each command.
|
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' |
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 |
Not surprising. Check the output at each stage and see what's happening at each pipe.
|
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/|//' Code:
cat example | awk '{print $1}' Code:
cat example | sed 's/*.com//g' Code:
cat example | sed 's/Stop*SS/example/g' |
Making some progress here with > $ cat test.txt | sed 's/^.* SS/example/' whose output is:
example -c -i567 -i8a9b" example -c -g123 -g456" |
Very good. Now instead of '^', as I understand it, you want to start with 'Stop'
|
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 -
Code:
sed 's/Stop.* SS/\/home\/joe\/test/' Code:
sed 's|Stop.* SS|/home/joe/test|' |
@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! |
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%' Code:
-r Use extended regular expressions (saves needing a lot of backslashes) |
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 |
Quote:
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%' |
Awesome feedback! Truly appreciate all your help!!!
|
All times are GMT -5. The time now is 09:51 AM. |