I'm trying to learn about regular expressions. Let's say I want to list all the files in /usr/bin beginning with "p", ending with "x", and containing an "a".
I know this works:
but I'm thinking there must be a way to do it without typing grep three times. Some of my attempts:
Ubuntu 18.04.2; Xfce 4.12.3; kernel 4.15.0-45-generic; bash 4.4.19(1); Dell Inspiron-518
I know this works:
but I'm thinking there must be a way to do it without typing grep three times. Some of my attempts:[CODE]
You are right and there is - in fact there are two ways:
First, you can use several expressions in grep at once by using the -e-switch:
will list all lines containing "one" AND all lines containing "two" from that file. It is in fact a logical OR for these two expressions.
The second possibility (and this is probably what you wanted) is to combine regular expressions. i.e. to get all lines containing "one" and "two" in that order you would write:
The regexp means: "one", followed by anything (".*"), followed by "two". If the order of the two words should not matter you need two regexps, which you can combine with the method above:
Search for "'one', something, then 'two'" or for "'two', something, then "one"'.
You can also use sed (the stream editor) for such (or even more complex) purposes where grep might get a bit unwieldy: sed -n will only print the lines you explicitly print (the default is to print every line after it is processed, which includes passing it through unchanged) therefore:
basically is the same as
But you can nest certain rules in sed which grep cannot do. i.e.
Would be similar to the example above: print only the lines containing "one", "two" and "three" but in any order.
I hope this helps.
bakunin
Last edited by bakunin; 02-22-2019 at 10:46 AM..
Reason: typo
These 4 Users Gave Thanks to bakunin For This Post:
@stomp--ls | grep "^p.*a.*x$" works great, with either single or double quotes. I'm used to thinking of the asterisk as a wildcard, which was confusing me at first.
@bakunin--this works great
Normally ls lists the contents of a given directory.
Say there would be a pax/ subdirecory beside the partx file,
ls p*a*x would list partx and the contents of the pax/
While ls -d p*a*x would list the two items partx and pax.
This User Gave Thanks to MadeInGermany For This Post:
Hello Everyone,
I have 4 different files (one column in each) that I'm trying to combine into 1 file with four columns. Having issues trying to get the columns to format properly. I have tried the following:
paste file1 file2 file3 file4 | column -s $'\t' -t > results.txt
paste file1 file2... (1 Reply)
i'm trying to optimize my script. i have a lot of instances where i'm doing something like this:
echo $blah | sed 's~ ~|~g' | sed 's-_space_- -g' | sed 's-_LP_-(-g' | sed 's-_RP_-)-g'
obviously, this is very inefficient.
i know i can combine into one sed command with something this, using... (7 Replies)
I have 2 files.
each having 3 coloums
1st field date as 20130322
2nd field time as 05:55
3rd field numberic value
File 2 has entries missing for some date time.
FILE1
20130322 05:35 2219
20130322 05:40 1809
20130322 05:45 1617
20130322 05:50 ... (2 Replies)
Hi everyone,
I'm new to the forums, as you can probably tell... I'm also pretty new to scripting and writing any type of code.
I needed to know exactly how I can grep for multiple strings, in files located in one directory, but I need each string to output to a separate file.
So I'd... (19 Replies)
I need to run a cronjob that will monitor a directory for files with a certain extension, when one appears I then need to run the below scripts How do I go about combining the following sed statements into one script? and also retain the original filename.?
sed 's/71502FSC1206/\n&/g' # add a... (2 Replies)
Hello, I am a new joiner to the forum, and have what i hope is a simple question, however I can't seem to find the answer so maybe it is not available within bash scripting.
I intend to use the below script to archive files from multiple directories at once by using a loop, and a variable (n)... (10 Replies)
I've been trying to find information in regard to creating a script that will generate HTML files. I currently have a series of files that contain code I need to surround with a <textarea> tag for easy viewing. I have about a thousand files that contain code, one file that contains the HTML code up... (10 Replies)
Hi Guys,
I am looking to optimze these 5 SSH lines to a single SSH to get my machine to not hang! lol!
cat hosts.lst | xargs -n1 -t -i echo 'home/util/timeout 6 0 ssh -q {} top -b > util/{}.top &' >> r_query_info
cat hosts.lst | xargs -n1 -t -i echo 'home/util/timeout 6 0 ssh -q {} uname -r... (5 Replies)
Before I ask my actual question, is it going to be a problem that I want to run this process on a 15 Gig file that is ~140 million rows?
What I'm trying to do:
I have a file that looks like
Color,Type,Count,Day
Yellow,Full
5
Tuesday
Green,Half
6
Wednesday
Purple,Half
8
Tuesday
...... (3 Replies)
I am fairly new to scripting. But I have been able to extract and format all of my information required into one file. My issue is that one character is on a separate line. I need to be able to add the character to the previous line.
ex.
abcdefghi
1
bcdefghij
3
cdefghijk
4
need to... (4 Replies)