I am trying to get strings that are separated by colons from a file with grep. I have managed fine so far but I ran into a problem where grep is just ignoring one of the characters I am trying to include in the search.
The file I am searching contains this line
configname:user:ip:password:macaddr
and the command I am running is grep -o "configname:.*:" sshutil_config
I thought this would find "configname:user:" but all it does is remove "macaddr" from the output. configname:user:ip:password:
The username will change and so I never know what it is so I can't grep specifically for it but I will know the configname and so I am trying to search for the username using it. I need to get the username out of the file as input and save it to a variable using usr=$(grep -o "whatever_this_needs_to_be" sshutil_config)
Thanks in advance,
-A\\/
Change "configname:.*:" to "configname:[^:]*:"
This is also possible if you don't want to use grep
user="$( cut -d ':' -f 2 <<< "$data" )"
Related
I have a test.txt file with links for example:
google.com?test=
google.com?hello=
and this code
xargs -0 -n1 -a FUZZvul.txt -d '\n' -P 20 -I % curl -ks1L '%/?=DarkLotus' | grep -a 'DarkLotus'
When I type a specific word, such as DarkLotus, in the terminal, it checks the links in the file and it brings me the word which is reflected in the links i provided in the test file
There is no problem here, the problem is that I have many links, and when the result appears in the terminal, I do not know which site reflected the DarkLotus word.
How can i do it?
Try -n option. It shows the line number of file with the matched line.
Best Regards,
Haridas.
I'm not sure what you are up to there, but can you invert it? grep by default prints matching lines. The problem here is you are piping the input from the stdout of the previous commands into grep, and that can lack context at grep. Since you have a file to work with:
$ grep 'DarkLotus' FUZZvul.txt
If your intention is to also follow the link then it might be easier to write a bash script:
#!/bin/bash
for line in `grep 'DarkLotus FUZZvul.txt`
do
link=# extract link from line
echo ${link}
curl -ks1L ${link}
done
Then you could make your script accept user input:
#/bin/bash
word="${0}"
for line in `grep ${word} FUZZvul.txt`
...
and then
$ my_link_getter "DarkLotus"
https://google?somearg=DarkLotus
...
And then you could make the txt file a parameter.
etc.
I tried to use grep to sub-sample a file using a pattern list in a second file. However, whenever I run it, grep just returns everything that is in the first file.
grep -A1 -w --file=Fmerg_U1_filtering.txt Fmerg_final.fasta
However, when I use this more cumbersome approach, it works perfectly fine.
#!/bin/bash
while read i; do
grep -A1 -w $i Fmerg_final.fasta
done < Fmerg_U1_filtering.txt
Any ideas what could be the problem? This is driving me crazy!
head Fmerg_U1_filtering.txt
Transcript_25
Transcript_455
Transcript_526
Transcript_631
Transcript_631
very likely, the text in your Fmerg_U1_filtering.txt has trailing/leading spaces.
Assume there were trailing spaces, your grep command with --file will try to match those spaces as well, however, your shell script with $i (without quotes) will ignore the spaces.
So please check the file Fmerg_U1_filtering.txt, remove leading/trailing spaces.
Okay I have a file that contains numbers like this:
L21479
What I am trying to do is use grep (or a similar tool) to find all the strings in a file that have the format:
L#####
The # will be the number. SO an L followed by 5 numbers.
Is this even possible in grep? Should I load the file and perform regex?
You can do this with grep, for example with the following command:
grep -E -o 'L[0-9]{5}' name_of_file
For example, given a file with the text:
kasdhflkashl143112343214L232134614
3L1431413543454L2342L3523269ufoidu
gl9983ugsdu8768IUHI/(JHKJASHD/(888
The command above will output:
L23213
L14314
L35232
If it is just in a single file, you can do something along the lines of:
grep -e 'L[0-9]{5}' filename
If you need to search all files in a directory for these strings:
find . -type f | xargs grep -e 'L[0-9]{5}'
so thanks to this forum, I currently have this code, which takes an output from a programme I have and saves it in a file:
#!usr/bin/python
import os
os.chdir('./P574/J0998-1034')
os.system('vap -c freq *.SFTC > 1400list.txt')
I wanted to add a filter (so take only lines that contained "1369.000", so I amended the last line to:
os.system('vap -c freq *.SFTC | egrep 1369.000 > 1400listfilt.txt')
But I really want it to take lines that contain EITHER "1369.000" OR "1433.000". I tried:
os.system('vap -c freq *.SFTC | egrep 1369.000|1433.000 > 1400listfilt.txt' )
But I got the error message: "sh: 1433.000: command not found
egrep: write error: Broken pipe"
How can I make it check for two values? Also.. is this the best way to do what I am trying to do?
Thank you!
I would surround the arguments in single quotes as such:
egrep '(1369.000|1433.000)'
The shell is telling you that it could not redirect the output of egrep to the program 1433.000 which doesn't exist.
I have the following file:
asdasd
asd
asd
incompatible:svcnotallowed:svc\:/network/bmb/clerver\:default
incompatible:svcnotallowed:svc\:/network1/bmb/clerver\:default
incompatible:svcnotallowed:svc\:/network2/bmb/clerver\:default
asdasd
asd
asd
as
And now suppose I have the two variables v1="incompatible:svcnotallowed:" and v2="svc\:/network1/bmb/clerver\:default".
I would like to search the entire file using v1 and v2. I know this is a problem caused due to the file having a'\' in it. I just dont know how to eliminate it. I have tried storing v1 and v2 (both variable contents and grep usage) using single quotes, but in vain.
This is the series of commands I have tried :
grep "$v1$v2" file
grep '$v1$v2' file
I need this to work in KSH
please let me know the right way to use grep in this scenario.
Thanks.
grep -F "$v1$v2" file should do the trick -- with the -F option, it treats the pattern as a fixed string, so backslashes don't get interpreted as escapes or backreferences.
But fgrep "$v1$v2" file would probably be the most portable solution. As tomkaith13 notes in his comment, the -F option to grep isn't universally supported. On Solaris, the default grep doesn't support -F, but the version in /usr/xpg4/bin does.
Since you are using ksh, you can just use it to read the files
v1="incompatible:svcnotallowed:"
v2="svc\:/network1/bmb/clerver\:default"
while read -r line
do
case "$line" in
"$v1$v2" ) echo "$line";;
esac
done < file