New answers tagged

0

comm -12 <(grep -oP '\w+' a|sort -u) <(grep -oP '\w+' b|sort -u) where: grep -oP '\w+' a|sort -u gets a sorted list o words in file a the some for file b comm -12 outputs common lines


0

You can do this with the following snippet grep -f file1 -o file2 | sort -u K00025 K00089


0

So I tried searching for a line that starts with 5 digits and ends with any letter like this: ^[0-9][0-9][0-9][0-9][0-9][A-Z]$ That pattern matches lines that contain only 5 digits and a (capital) letter. If you expect there to be more between them, you will need to include it in the pattern. If you don't care what goes between them, use .* to ...


2

You can use grep -B2 -E '^[0-9]{5} +[a-zA-Z]+$' to try to find only address blocks. Some notes: see man grep to get an understanding of the options see the end of the manpage for grep to find a manpage that explains the regex syntax in detail, the GNU grep manpage itself also explains regex a little -B is "lines before the match" and might be better ...


0

Unfolding that one-liner and rearranging a bit, plus a few tweaks, gets: cat /dev/urandom | \ tr -dc 'a-zA-Z0-9' | \ fold -w 16 | \ tr -d '[A-z]' | \ grep '....' | \ head -n 16 Outputs: 7405935 60722 11225 96954 3966 8774 539418 1964 59150 5994 1086 7470 2751 8534 21501 14927 Note: the n-digit numbers are probably random if taken ...


0

To get the familiar regexps working you need to enable "extended regular expressions" with the flag '-E'. With that, your regexp should work: ... | grep -E -o '[0-9]{4,16}' The -P flag (Perl-compatible regular expressions), which some distributions support, is not necessary in this case.


0

sed solution: $ str='Hellowww.hello.comMywww.world.comWorld' $ echo "$str" | sed -e 's/com/com\n/g' | sed -ne '/.*www\.\(.*\)\.com.*/{ s//\1/p }' hello world


1

$ echo "www.blablabla.com" | grep -oP '(?<=\.)[a-zA-Z0-9\.-]*(?=\.)' blablabla -o -- print only matched parts of matching line -P -- Use Perl regex (?<=\.) -- after a literal ., aka, a "positive look-behind" ... [a-zA-Z0-9\.-]* -- match zero or more instances of lower & upper case characters, numbers 0-9, literal . and hyphen ... (?=\.) -- ...


1

perl -00 -n -e 'print if (m/blue/i && m/green/i && m/yellow/i)' filename This uses perl's paragraph-reading mode (-00) to print only paragraphs containing all three words (with case-insensitive matches). a 'paragraph' is one or more lines of text, separated from other paragraphs by at least one blank line. e.g. I saved the text of your ...


2

Even though this is an old question, it seems to me it's a perennial question, and a more general, clearer solution is available than has been suggested so far. Credit where credit is due: I'm not sure I would have come up with it without considering Stéphane Chazelas's mention of the <> update operator. Opening a file for update in a Bourne shell ...


0

There are 2 ways I recommend going about this. 1) Put a function in your bashrc / bash_profile and create an alias to call that function (this will make global usage of this) 2) create a shell script file and can create an alias of that file as well. #!/bin/bash function matchString(){ REGEX="$1" FILE="$2" RESULTS=$(grep -n "$REGEX" $FILE | awk -F ":" ...


0

This is a small improvement to Gilles awk solution (thanks Gilles!), but does require nawk: nawk '{if (/\\$/) {$0=substr($0,1,length($0)-2); printf "%s",$0} else print}' This will create a continuous line if the line wraps, but does not include the "\" and space character. (I found this helpful when grepping for PATH statements since the "\" can lead to ...


29

There are two problems with your example. The primary one is that you're assuming that regular expressions work the same as glob patterns in that * is a wildcard meaning "any sequence of characters." In regular expressions, * means "any number of the previous atom" instead, so fil* means f followed by i followed by zero or more l characters. You need to say ...


0

Also, if you just want to exclude some lines ahead of a given marker, you could use: awk -v nlines=2 '/Exception/ {for (i=0; i<nlines; i++) {getline}; next} 1' (glenn jackman at http://stackoverflow.com/a/1492538 ) By piping some commands you can get the before/after behaivour: awk -v nlines_after=5 '/EXCEPTION/ {for (i=0; i<nlines_after; i++) ...


1

You can reach a good-enough result by using temporary files: my_file=file.txt #or =$1 if in a script #create a file with all the lines to discard, numbered grep -n -B1 -A5 TBD "$my_file" |cut -d\ -f1|tr -d ':-'|sort > /tmp/___"$my_file"_unpair #number all the lines nl -nln "$my_file"|cut -d\ -f1|tr -d ':-'|sort > /tmp/___"$my_file"_all #join the ...


0

Here's another way with join/sort/uniq: join -1 1 -2 2 -a1 -e "0" -o 1.1 2.1 <(sort file1) \ <(cut -d' ' -f1 file2 | sort | uniq -c) Basically, it joins the names in file1 with the unique names and counts from file2 using 0 for the missing fields in file2. With awk I would run: awk 'NR==FNR{s[$1]++;next} {if ($1 in s) {print $1, s[$1]} else ...


2

test.awk: FNR == NR{ names[$1] next } ($1 in names){ ulog[$1]++ } END{ for(name in ulog){ print name ":" ulog[name] } } and run it as awk -f test.awk user.list user.log FNR==NR # does the file record number == the record number, if it does then we are still in the first file next # as we are still in the first file, skip the ...


2

Something simple like: mapfile -t names < file1 for name in "${names[@]}" do echo "${name}" $(grep -c "^$name " file2) done Will provide output like: Peht 2 Mawo 3 Stso 1 Makr 0 Bavo 2 The grep string says to anchor the username at the beginning (^) of the line, and enforce a trailing space after the line.


1

Not sure if this is too old to answer (first time answering a question), but the AIX equivalent you're looking for is ps -ef | awk '$NF~/[o]ra_pmon/ {print $2,$NF}' e.g. [oracle@aixbox ]$ ps -ef | awk '$NF~/[o]ra_pmon/ {print $2,$NF}' 8061108 ora_pmon_XXX 38993950 ora_pmon_YYY


1

Change ligne=`cat /var/log/svlog | grep "\$day"` to ligne=$(grep "$day" /var/log/svlog) To feed the contents of $ligne to awk, use echo "$ligne" | awk ...


2

You can use either of these, depending on what you're trying to display: $ echo "lol llol" | grep -E "\blol" lol llol $ echo "lol llol" | grep -Eo "\blol" lol Putting the regex in quotes solves your matching problem. The -o flag causes grep to only print the matched string instead of the entire line.


1

There are two main reasons for using cat as in your first example: As a place-holder for some other command or long and complicated pipeline of commands. e.g. if you're writing a script or one-liner to process a huge file, or data from a psql/mysql or wget or jq etc query, you might save (some of) the input to a sample file and use cat sample as input ...


6

In your first example cat f.txt | grep "someText" grep doesn't get a filename argument, only a string to search for. In that case grep will read the text to search from standard input. In this case that standard input is piped in from the output of cat f.txt, which outputs the content of the file not the filename. What you also could have done to make ...


3

Using "awk" This will print lines with N0 < LIMIT: # -v sets variables which can be used inside the awk script awk -v LIMIT=10 ' # We initialize two variables which hold the two previous lines # For readability purposes; not strictly necessary in this example BEGIN { line2 = line1 = "" } # Execute the following block if ...


1

For the record, you could also do this with sed: sed -s '$!N /.*PATTERN.*\n/{/\n.*PATTERN/{x;/^1$/!s/.*/1/;b v};//!{x;/^1$/{s/./0/;b v};//!D}} //!{${/PATTERN/{x;/^1$/{b v}}};D;};: v;x;P;D' file1 file2 ... fileN That's gnu sed. With other seds you'd have to process one file at a time: sed '$!N # if not on the last line pull in the next ...


1

In general I would recommend the answer by Gilles. If you need your script to be POSIX compatible and your ps does not support -u, this could work: ps -ef | grep -e \ "^smtpd ^dbus ^ntp" I.e. pipe the output of ps -ef through grep -e pattern_list. The pattern_list is supplied as a multiline-string, each pattern starts with ^ to anchor it at the ...


3

Short answer: ps -u user1,user2,user3 Your grep command is incomplete since the -f option requires an argument. Your cat command is receiving its input from the here document, so even if you fixed the grep command, its output would be discarded. If you were looking for a hard-coded user, you'd use ps -ef | grep alice (except that this isn't a good way ...


2

I'll use the same test file as thrig: $ cat file a pat 1 pat 2 b pat 3 Here is an awk solution: $ awk '/pat/ && last {print last; print} {last=""} /pat/{last=$0}' file pat 1 pat 2 How it works awk implicitly loops over every line in the file. This program uses one variable, last, which contains the last line if it matched regex pat. ...


1

#!/bin/bash # Array of root folders folders=("a" "b") # Search all specified root folders for f in ${folders[@]}; do # Descend hierarchy and retrieve modification date of each file with "stat" find $f -type f -exec stat -f "%m,%N" {} ';' | \ # sort by date, most recent first sort -gr | \ # extract first (most recent) file ...


2

One way would be to save the previous line, and print when the current and previous line both match: bash-4.1$ (echo a; echo pat 1; echo pat 2; echo b; echo pat 3) a pat 1 pat 2 b pat 3 bash-4.1$ (echo a; echo pat 1; echo pat 2; echo b; echo pat 3) | \ perl -nle 'print "$prev\n$_" if /pat/ and $prev =~ /pat/; $prev=$_' pat 1 pat 2 This will ...


0

With gnu you could try: find "$(pwd)" -mindepth 2 -maxdepth 2 -type d -printf "d%h\0%T@ %p\0" | awk -v RS="\0" ' /^d/ {directoryname=substr($0,2)} /^[0-9]/ && (!lmtimes[directoryname] || lmtimes[directoryname] < $1) { lmtimes[directoryname]=$1; lmtimedns[directoryname]=substr($0,index($0," ")+1); } END {for (directoryname in ...


-2

Hi there are various command which can help you fine last line try this.. <grep command> | tail -1 or awk '/result/ { save=$0 }END{ print save }' filename


0

sed -n "s/^.*foobar\s*\(\S*\).*$/\1/p" -n suppress printing s substitute ^.* anything before foobar foobar initial search match \s* any white space character (space) \( start capture group \S* capture any non-white space character (word) \) end capture group .*$ anything after the capture group \1 substitute everything ...


3

Awk is good for this sort of thing: awk '$1>=1460333000 && $1 <=1460417100' $1 is the first field.


0

There are a lot of good answers here that helped me get going but I ended up with this simple command: zgrep -e 'something to search for' *.gz


0

If you only want the text in between ] and apal, perl is a good choice perl -lne '/(?<=\])(.+?)(?=apal)/ and print $1' file If you have GNU grep installed (via homebrew for example), you can grep -Po '(?<=\]).+?(?=apal)' file


1

Following up on @cas's comment to another answer: six_months=$(date -d "6 months ago" "+%Y%m%d") for f in *.Txt; do file_date=${f%.Txt} [[ $file_date > $six_months ]] && echo "$f" done | xargs awk 'FNR > 1 {print $3}' | sort -u > unique_ids_in_last_6_months The for loop prints out the "eligible" filenames. xargs passes the ...


6

You might need a word stemming algorithm for this. For example, Lingua::Stem is a word stemmer module written in Perl. If this fits your needs, you would need to install Lingua::Stem via CPAN. Then, the following Perl script would do the job: #!/usr/bin/perl require Lingua::Stem; # Read lines into array chomp(my @words = <STDIN>); # Stem in ...


1

Use awk to do the work, and SQLite for the dates. sqlite3 <<< "select date('2016-04-20', '-6 month');" 2015-10-20 Dandy, ain't it? awk has string functions to insert/delete the hyphens SQLite needs and, yea, splits on tab delimiters. awk 'NR == 1 {next}; { IDS[$3]++ }; END {for (K in IDS) {print K}}' ids 123456 345678 234567 Guaranteed ...


1

If you would have taken the time and read the grep manual, you would have found the l option -l, --files-with-matches Suppress normal output; instead print the name of each input file from which output would normally have been printed. The scanning will stop on the first match. Your find command would look like find . -name "*.tex" -exec ...


2

It is not trivial to find the exact date 6 months ago, especially if the current date would be the 31st of some month. But if you know how to do this with find and -mtime, I would just touch the files depending on the date in their name: for x in *.Txt; do dd=${x%.Txt} touch -t "$dd"0000 "$x" done and then use the mtime


3

echo -n "$filename|"; tr "\n" "," <"$filename"


4

export myvar awk 'substr($0, 42) == ENVIRON["myvar"]' < "$filelist"


3

This is a different way to go; it first sets 'line' to be empty, and sets it if it finds that 'myvar' matches the trailing 3 fields. line= while IFS=' ' read -r hex int1 int2 rest do if [[ "$myvar" = "$int1 $int2 $rest" ]] then line="$hex $int1 $int2 $rest" fi done < filelist Here's yet another way, using bash's mapfile builtin and second ...


6

Perl to the rescue! perl -ne 'BEGIN { $search = shift } print if /^.{41}\Q$search\E$/; ' -- "$myvar" "$filelist" -n reads the file line by line. The BEGIN block retrieves the $myvar from the first argument into Perl variable $search. \Q...\E quotes the inner part (see quotemeta). This handles all the special characters the variable can ...


2

If you are set on doing this in sed and matching on the tex: instead of just :, you can also try: echo "./BitTorrentSync/Gyn/1.12.2015.tex: Agents in young <40yr?" | sed 's/\(^.*tex\):.*/\1/'


1

Use grep with -o to get the desired portion only: grep -Eo '.{,50}text_string.{,50}' filename.sql


1

$ abc="./BitTorrentSync/Gyn/1.12.2015.tex: Agents in young <40yr?" $ pqr=$(echo "$abc" | sed -e 's/:.*//') $ echo $pqr ./BitTorrentSync/Gyn/1.12.2015.tex This works for me.


5

sed operates on stdin, not on its arguments, unless you are giving it filenames. It's easier to specify what you want to remove than what you want to keep, with sed. Instead of sed -n 's/.*tex:/[preventColonFromResult]/p' ./BitTorrentSync/Gyn/1.12.2015.tex: Agents in young <40yr? perhaps you meant printf '%s\n' './BitTorrentSync/Gyn/1.12.2015.tex: ...


1

echo "bla-bla-bla:ololo-testo" |sed 's/:.*$//' bla-bla-bla



Top 50 recent answers are included