grep giving error - linux

I am trying to extract no.s from a file, so I created a script, but grep is giving error:grep: line too long. Can anyone tell me where am I wrong. command is:
echo $(cat filename|grep '\<[0-9]*\>')
Thanks in advance

grep is line-oriented; it will print matching lines to output. Probably you have a huge line in your file, and the resulting line cannot be converted into a string value by shell, as $(...) requires.
First of all, try just cat filename | grep '\<[0-9]*\>' > results and see what is in the results file. Maybe it's enough.
But if you have multiple numbers in a line and you want to extract them all, use -o: grep -o '\<[0-9]*\>'. This will print only matching parts, every match on a new line, even if original matches are on the same line. If you need line numbers, too, add -n.

Related

Bash script - Get part of a line of text from another file

I'm quite new to bash scripting. I have a script where I want to extract part of the value of a particular line in a separate config file and then use that value as a variable in the script.
For example:
Line 75 in a file named config.cfg
"ssl_cert_location=/etc/ssl/certs/thecert.cer"
I want just the value at the end of "thecert.cer" to then use in the script. I've tried awk and various uses of grep but I can't quite get just the name of the certificate.
Any help would be appreciated. Thanks
These are some examples of the commands I ran:
awk -F "/" '{print $4}' config.cfg
grep -o *.cer config.cfg
Is this possible to extract the value on that line and then edit the output so it just contains the name of the certificate file?
This is a pure Bash version of the basic functionality of basename:
cert=${line##*/}
which removes everything up to and including the final slash. It presupposes that you've already read the line.
Or, using sed:
cert=$(sed -n '75s/^.*\///p' filename)
or
cert=$(sed -n '/^ssl_cert_location=/s/^.*\///p' filename)
This gets the specified line based on the line number or the setting name and replaces everything up to and including the final slash with nothing. It ignores all other lines in the file (unless the setting is repeated in the case of the text match version). The text match version is better because it works no matter what line number the setting is on.
grep uses regular expressions (as does sed). The grep command in your command appears to have a glob expression which won't work. One way to use grep (GNU grep) is to use the PCRE feature (Perl Compatible Regular Expressions):
cert=$(grep -Po '^ssl_cert_location=.*/\K.*' filename)
This works similarly to the sed command.
I have anchored the regular expressions to the beginning of the line. If there may be leading white spaces (the line may be indented), change the regex so it looks something like this:
^[[:space:]]*ssl_cert_location=
which works for both indented and unindented lines.
There are many variants, but a simple one that comes to mind with grep is first getting the line, then matching only non-slashes at the end of the line:
<config.cfg grep '^ssl_cert_location=' | grep -o '[^/]*$'
Why didn't your grep command (grep -o *.cer config.cfg) work? Becasue *.cer is a shell glob pattern and will be expanded by the shell to matching file names, even before the grep process is even started. If there are no matching files, it will be passed verbatim, but * in regular expressions is a quantifier which needs a preceeding expression. . in regex is "match any single character". So what you wanted is probably grep -o '.*\.cer', but .* matches anything, including slashes.
An awk solution would look like the following:
awk -F/ '/^ssl_cert_location=/{print $NF}' config.cfg
It uses "/" as separator, finds only lines starting with "ssl_cert_location" and then prints the last (NF) field in from this line.
Or an equivalent sed solution, which matches the same line and then deletes everything including the last slash:
sed -n '/^ssl_cert_location=/s#^.*/##p' config.cfg
To store the output of any command in a variable, use command substitution:
var="$(command with arguments)"

How to check if lines in a file are formatted correctly

What command would I use on linux to see if the lines in a file are correctly formatted? So if a text file (names.txt) had the lines;
{"first":joe "last":smith} and "{"first:joe last":smith] it would show me that the first line is correct and the 2nd line is not.
You could use a simple grep like this:
grep -v -E '^{".*?":.*? ".*?":.*?}$' names.txt
Assuming, that your example provided is no real JSON (missing the comma after the first value).

bash: write all lines starting with one of strings

I have a set of strings and want to write out all lines of a file that start with either of these.
Tried this that I found on the internet but that writes out the whole file... May I ask for a help? Thanks!
grep -Ev "^(58|11518|11909|11910|11911|11912|11913|11914|11915|11916|11917|11918|11919|11920|11921|11922|11923|11924|11925|11926|12055|12056|12060|12102|12103|12104|12105|12106|12107|12108|12109|12110|12111|12112|12113|12114|12115|12116|12117|12118|12119|12120|12121|12122|12123|12124|13813)" dead_end1_model.inp > newfile.txt
Your current invocation of grep only prints the inverse -v of the matches. I.e. any line not starting with any of those strings gets matched.
By your description you want to match the lines that do start with any of the strings, so just remove the -v from the invocation.

How can replace a specific line in a text file with a shell script?

I am trying to replace a specific line in a txt file with my shell script, for example;
cat aa.txt:
auditd=0
bladeServerSlot=0
When I run my script I would like to change "bladeServerSlot" to 12 as following;
cat aa.txt:
auditd=0
bladeServerSlot=12
Could you please help me?
Using sed and backreferencing:
sed -r '/bladeServerSlot/ s/(^.*)(=.*)/\1=12/g' inputfile
Using awk , this will search for the line which contains bladeServerSlot and replace the second column of that line.
awk 'BEGIN{FS=OFS="="}/bladeServerSlot/{$2=12}1' inputfile
perl -pe 's/bladeServerSlot=\K\d+/12/' aa.txt > output.txt
The \K is a particular form of the positive lookbehind, which discards all previous matches. So we need to replace only what follows. The s/ is applied by default to $_, which contains the current line. The -p prints $_ for every line, so all other lines are copied. We redirect output to a file.
Is it really necessary to replace the line in your example? As bladeServerSlot is a variable you could reset the value.
bladeServerSlot=`any command`
Or you could just let this variable be filled by a Parameter provided to this script.
bladeServerSlot=$1
With $1being the first parameter of your script. I think this would be the cleaner way do solve your issue than to do fancy regex here. The sed/perl solutions will work, but they are not very clear to other people reading your script.

Print the line and a line 10 lines before the match in Linux

I want to match a string in a file, print the line which matches and the line 10 lines before the match occurs. I am trying using awk, sed and grep, but cant get the right thing. Can anyone help?
Try this command:
grep -B 10 PATTERN file.txt
The -A and -B options to GNU grep allow you to specify how much context should be displayed after and before the matching line.

Resources