New answers tagged

1 vote

Print ONLY the last line after a match with awk

If sed is an option $ sed -En '/Artifacts were created:/{n;s/[^ ]* (.*)/\1/p}' <<< "$file" build-1.tar.gz
  • 2,129
1 vote
Accepted

Print ONLY the last line after a match with awk

Please try: awk '/Artifacts were created:/{p=1} NF{out=$2} END{if(p==1){print out}}' infile Although the pipe you presented is extracting the second filed of the line just following the match, not ...
0 votes

Print ONLY the last line after a match with awk

The original requirement asked for a value from the last non-whitespace line after a match. Here, when you find a match trigger, set a flag. When the flag is set wait for the end of file and then ...
  • 93.7k
0 votes

How to search file name just created by some one on Linux server

If you want today as in after midnight, this should work (not DST safe!): find /path/to/directory -name \*.tar -mmin "-$(( $(date "+10#%H * 60 + 10#%M") ))" The time calculation ...
  • 5,809
2 votes
Accepted

Bash Function Grep Mod

In bash and sh "$@" will expand all positional parameters so: cgrep () { clear clear grep "$@" } Note that $@ has to be double quoted to prevent word splitting, globbing, ...
  • 33.8k
1 vote

AWK or grep with cut. Need help cutting a field

awk can do what grep and cut do, and awk is more flexible by default than cut about its field separators awk '/^2014-/ {print $3}' filename
0 votes

AWK or grep with cut. Need help cutting a field

I believe your problem is that you have a whitespace after - and before f: grep '^2014-' FileName | cut - f 3| less ^ here It should be: grep '^2014-' FileName | cut -f ...
1 vote

Determine whether strings in file exist or not in file hierarchy

The -l option will generally print the files' names matching the search term(s). The -r option will allow grep to also go inside each directory found. An option of -f will tell grep that what is given ...
0 votes

Validating only four digits using SED

I find it much harder to understand sed speak. I prefer awk for most things. Given that, one solution to your problem is the following script which, as you can see, can make the logic more explicit ...
5 votes

Inverse grep does not find all matching lines

With the pattern files $ od -bc shortlong 0000000 167 150 157 012 167 150 157 163 145 012 w h o \n w h o s e \n 0000012 $ od -bc longshort 0000000 167 150 157 163 145 ...
  • 33k
8 votes

grep $? -ne 0 breaks script

The main issue with your code is a missing $ in the test ({gret} should be ${gret}, or better yet, "$gret"), and that you never assign the exit status of grep to the gret variable (which is ...
  • 281k
1 vote

Extract all subnets from its first common octet

Print each line where the first dot-separated field is 1: awk -F. '$1=="1" {print}' datafile Since each line of an awk script is condition {action}, and either part can be omitted for a ...
  • 93.7k
2 votes
Accepted

Extract all subnets from its first common octet

This will do what you want: grep '^1\.' filename The ^ symbol indicates the beginning of the line, and \. means literal dot.
0 votes

List all the words starting with $ symbol

The problem is that \b is a zero-width boundary between a word-char and a non-word char. $ is not a word-char You probably need: grep -Po '(?<=^|\W)\$\w+' /var/www/html/my_site/index.php which ...
1 vote
Accepted

How to match end of last string in file which does not end with punctuation

This could be done with GNU sed and both the option to treat a file list as separate files and the command F to print filename. $ sed -sn '${s/.*\(.\)$/\1/;/[^[:punct:]]/{F;l}}' ./* ./fileone a$ ./...
3 votes

Arranging the lines in required order

You can always use a decorate-sort-undecorate approach where you assign -2 to t, -1 to b with: <your-file awk -F'[<>]' ' {print $2 == "t" ? -2 : $2 == "b" ? -1 : $2, $0}'...
2 votes

How to match end of last string in file which does not end with punctuation

This may be what you're trying to do, using GNU awk for ENDFILE: awk 'ENDFILE { if ( FNR ) { c = substr($0,length(),1) if ( c !~ /[[:punct:]]/ ) { print FILENAME, c ...
  • 24.5k
0 votes

Arranging the lines in required order

There may be other ways, but the best way I can think to do this involves using multiple commands, which you can do with a small script. The script here first gets all lines with <t>, then gets ...
  • 7,582
1 vote

How to match end of last string in file which does not end with punctuation

In case you need to find file contents ending without punctuation: for f in ./* ; do tail -n 1 "$f" | grep -qv '[^[:punct:]]$' || echo "$f" ; done In case you need to find ...
  • 303
0 votes

How to use sed, grep, or awk to keep certain lines in a file depending on line numbers in another file

in case the target line numbers in File2 are in increasing order, to this approach will do. sed -e 's/$/b/;$a d' < File2 | sed -f - File1 Generates a series of sed commands 1b 4b d In the ...
  • 5,606
5 votes
Accepted

Why does this regular expression match odd files?

An optional match like (ccdd)? is exactly that: optional. It could happen that the matched text is empty, but still, that is a match, a line that has a match. $ ls -x aa aaaa aabb aabbaa ...
1 vote

Why does this regular expression match odd files?

grep isn't matching the files like errsort .... it's matching the line, and the only thing it actually matches is the (aa) (everything else is optional because of the ?). Because you chose ls -x the ...
  • 5,809

Top 50 recent answers are included