grep for line containing jboss- until next / - linux

I am trying to grep out of a init script status, just the jboss directory.
So in the stdout there is this line:
JBOSS_CMD_START = ulimit -c 2500000; cd /home/blah; /apps/jboss-eap-5.1.2/jboss-as/bin/run.sh -c jboss-blahtest -b 1.1.2.3 -Djboss.messaging.ServerPeerID=1
And out of that, I am trying to grep just the directory, up until /jboss-as, so the results would be:
/apps/jboss-eap-5.1.2/jboss-as/
The problem is the jboss version can be a number of things, so I need to get from /apps/jboss- to /jboss-as/

grep -oE '/apps/jboss-eap-[^/]+/jboss-as/'

One option
grep -Eo '/[^[:space:]]+jboss-as/'

grep -oE '\S+/jboss-as/ should do it.

Related

cat: pid.txt: No such file or directory

I have a problem with cat. I want to write script doing the same thing as ps -e. In pid.txt i have PID of running processes.
ls /proc/ | grep -o "[0-9]" | sort -h > pid.txt
Then i want use $line like a part of path to cmdline for evry PID.
cat pid.txt | while read line; do cat /proc/$line/cmdline; done
i try for loop too
for id in 'ls /proc/ | grep -o "[0-9]\+" | sort -h'; do
cat /proc/$id/cmdline;
done
Don't know what i'm doing wrong. Thanks in advance.
I think what you're after is this - there were a few flaws with all of your approaches (or did you really just want to look at process with a single-digit PID?):
for pid in $(ls /proc/ | grep -E '^[0-9]+$'|sort -h); do cat /proc/${pid}/cmdline; tr '\x00' '\n'; done
You seem to be in a different current directory when running cat pid.txt... command compared to when you ran your ls... command. Run both your commands on the same terminal window, or use absolute path, like /path/to/pid.txt
Other than your error, you might wanna remove -o from your grep command as it gives you 1 digit for a matching pid. For example, you get 2 when pid is 423. #Roadowl also pointed that already.

grep the lines of file using for loop

There is a file and each line shows the different file paths.
```
my_file
/the/first/path/file1
/the/second/path/file2
....
```
I want to use for loop to find a string in each file.
```
for i in $(cat my_file); do cd "$i"; done | grep -w string "$i"
```
But this seems not working for me. I am getting this for all file directory
-bash: cd: /the/first/path/file1: No such file or directory
What am I doing wrong? Thanks in advance for any help
No need for a for loop, use xargs:
xargs -a my_file -d '\n' grep -h -w string
Note #1: I added the -h option (GNU extension) so that the filenames are not added by grep in the output (like in your command).
Note #2: since you are using Linux and Bash, I'm assuming GNU xargs. If your xargs does not understand the -a option, then use this instead:
< my_file xargs -d '\n' grep -h -w string

Shell script to pass the output of first command to the next in pipe

I would like to grep exception or error from logs, but the problem is that the exact log file name is unknown. One thing for sure is that the latest file is my log file and i want to do this single command since, i'll be using the command to do ssh from single source to multiple servers
like
ssh user#server "ls -ltr console*.log | tail -1; egrep -i 'exception|error' <<output of first command (i.e) log file name>>"
is this possible to do in single command ??
Does this work for you:
egrep -i 'exception|error' < $(\ls -tr console*.log|tail -1)
to get only the log name do not use -l in ls
Thanks everyone, i finally got it worked
ssh user#server "ls -ltr console*.log | tail -1; awk '{print $9}' | xargs egrep -i 'exception|error' <<output of first command (i.e) log file name>>"

Output 'man grep' command into a file

I have practiced in using command line in CentOS6.
I tried to create file, which content would be the output of command man grep. Also I used command man with col -b option to save file as Text-Only. All of this must be in one command.
I tried to do like this:
grep man grep | col -b > output.txt
But it didn't work.
What is the proper way to save output of command man grep as Text-Only file with using option col -b?
Don't you really need this:
man grep | col -b > output.txt
Why do you need to call grep in the first place?
Other, hacky way using grep:
man grep | grep -v somephrasethatwontoccur | col -b > output.txt
But, truly, it makes no sense. grep -v looks for lines without the specified phrase.

how can I extract a pattern only using grep

How can we extract the contents present inside KeyProviderType tag only using grep command from the folllowing pattern?
<ContentProtectKeyProfiles-row><Name>PREM7</Name><Domain>42.0.112.121</Domain<ProfileType>4</ProfileType>
<Protocol>HTTP</Protocol><Port>80</Port><KeyProviderType>HLS-AES-128</KeyProviderType</ContentProtectKeyProfiles-row>
a#x:/tmp$ cat s.xml
<ContentProtectKeyProfiles-row> <Name>PREM7</Name> <Domain>42.0.112.121</Domain> <ProfileType>4</ProfileType> <Protocol>HTTP</Protocol> <Port>80</Port> <KeyProviderType>HLS-AES-128</KeyProviderType> </ContentProtectKeyProfiles-row>dhruv#dhruv-pathak:/tmp$
a#x:/tmp$ cat s.xml | grep -oe "<KeyProviderType>.*</KeyProviderType>"
<KeyProviderType>HLS-AES-128</KeyProviderType>
Don't use grep to process XML files. Use a proper XML parser. For example, using xsh, I can just run
open in.xml ;
echo (//KeyProviderType) ;
BTW, I had to fix 2 tags that were missing > in your input.
You can try to use gnu awk (due to RS)
awk -v RS="KeyProviderType" 'NR%2==0 {gsub(/>|<\//,"");print}' file
HLS-AES-128
You may use regex lookahead's and lookbehind's, provided your grep support -P flag.
cat s.xml | grep -oP "(?<=<KeyProviderType>).*(?=</KeyProviderType>)"

Resources