Hot answers tagged

5

In awk, you could collect the line numbers to an array and read through the file once, printing the lines that are mentioned in the array: #!/bin/sh awk -v lines="$*" 'BEGIN { split(lines, a, "[, ]"); for (i in a) b[a[i]] = 1;} NR in b {print $1, $2}' < data.txt The split() splits the ...


4

Consider this: #!/bin/bash - patterns=('pattern1' 'pattern2' 'pattern3' 'pattern4' '...') for pat in "${patterns[@]}"; do if ! grep -q -e "$pat" infile; then ((missed++)) printf '%s\n' "pattern $pat not found" fi done if [[ -z "$missed" ]]; then printf '%s\n' 'all patterns were found' else ...


4

You can use vgs and tailor the output format instead, to reduce the number of steps: sudo vgs --units b --no-suffix --noheadings -o vg_name,vg_size,vg_free | awk '{ printf "%s %f%%\n", $1, $3 * 100 / $2 }' This forces output in bytes (--units b), removes suffixes and headings, and outputs only the VG’s name, total size, and free space, before ...


2

words=(hello world foo bar) grep -F -f <(printf "%s\n" "${words[@]}") text.txt This uses process substitution to provide a "file" containing each word on its own line. The -f option tells grep to read its list of patterns to match from a file. The -F option tells grep that the patterns are fixed strings, not regular ...


1

This modified script adds support for single numbers: #! /bin/bash civic="$1" street="$2" if [ "$((civic%2))" = 1 ]; then exclude=" even " else exclude=" odd " fi </path/to/addresses.txt grep -E "(^| )$street" \ | grep -v "$exclude" \ | awk -F '[ -]' -v civic="$civic&...


1

With awk and a bash wrapper. Save it as script.sh and make it executable. #!/bin/bash filename="data.txt" n="$1" # save number from argument list shift # remove number from argument list s="$@" # save remaining argument list s="${s:=.*}" # set regex .* as default if street is missing awk -v number=...


1

#! /bin/bash civic="$1" street="$2" if [ "$((civic%2))" = 1 ]; then exclude=" even " else exclude=" odd " fi </path/to/addresses.txt grep "$street" \ | grep -v "$exclude" \ | awk -F '[ -]' -v civic="$civic" ' {if ($1 !~ /^[0123456789]*$/ || $2 !~ /^[...


1

You want grep -E '\D\d{7}$' a non-digit followed by 7 digits and the end of the line. This would also work awk 'length($NF) == 7 && $NF !~ /[^0-9]/' last field is 7 characters long and does not contain a non-digit. A couple of the errors in your samples: grep '^.......' -- the line starts with 7 of any character grep -E '^.{7}$' -- line is ...


1

Pass the file containing your patterns into grep as following, to look for any of those pattern within multiple files you can specify like below: grep -f pattern.file multiple.file* if you only want match the patterns on an entire line not partiall match as above does, and for that you can add -x option to the grep.


1

#!/bin/bash perl -le ' for (@ARGV) { # separate command line args into filename(s) and line-number(s) # line-numbers can be space and/or comma separated. if (-e $_) { push @files, $_ } else { push @lines, split /,/}; }; @ARGV = @files; $re = join("|",@lines); while(<>) { print join("\t",(split)[0..1]) if ($. =~ m/^($re)$/)...


1

The following bash script builds a sed script that will modify and display the lines by the numbers provided on the command line: #!/bin/bash sed_script=() for lineno do sed_script+=( -e "$lineno ba" ) done sed "${sed_script[@]}" \ -e 'd' -e ':a' -e 's/[[:blank:]]*[^[:blank:]]*$//' <data.txt or, for /bin/sh: #!/bin/...


1

Put the below lines in a text file and name it as get.sh. then make it executable . #!/bin/sh ## this is GNU sed sed -En " $(printf '%sbp\n' "$@" "d;") :p;s/\S+/&\n/2;P " data.txt Now invoke the script as shown: chmod +x ./get.sh ./get.sh 1 3 5


Only top voted, non community-wiki answers of a minimum length are eligible