Tag Info

New answers tagged

1

awk awk 'FNR==NR { a[$2, $3]=$4 next } ($2, $3) in a{ print $0, a[$2, $3] } ' file2.txt file1.txt > out.txt join join -j 2 \ <(sort -k2,3 file2.txt | sed 's/ /+/2') \ <(sort -k2,3 file1.txt | sed 's/ /+/2') \ -o ...


1

In your example you are creating array m with values 0. You nowhere set it to 1, and you don't need to. You can just set value to entire row, e.g $0. Try this: awk 'FILENAME == ARGV[1] { m[$2,$3] = $0; next; } { if (($2,$3) in m) { print m[$2,$3] " " $4 >"out.txt"; } }' file1.txt file2.txt


-1

echo -e 'one two three\nfour five six\nseven eight nine' |awk -F " " '{print $NF }'


1

I found a solution using perl substring function echo leng-1323-006|perl -lpe 'substr($_, 5, 5) = "";' return correctly leng-006 I hve removed from position 5 the 5 bad characters include the -


1

POSIXly: $ sed -e 's/-[^-]*-/-/' file leng-001 leng-002 leng-004 leng-005 leng-006


2

Using sed: sed -r 's/(.*[a-zA-Z]{4}\-)([0-9]{4})/\1aaaa/g'


0

Try something like wget -qO- http://www.google.com/ | tr " " \\n | fgrep src= | tr \" \\n | fgrep -v src= which will, however, output URLs as they are written in the file - which may be relative the the base URL.


2

If your pax supports the -0 option: find . -type 'f' -path '*downloads*' -print0 | pax -rw0s'|.*downloads/||' /mydir


0

This should do it: for file in /Module*/*/downloads/m13312/file.pdf do file_directory="/mydir/$(basename "$(dirname "$file")")" mkdir -p "$file_directory" cp "$file" "$file_directory" done


1

If there is no other limitation here your script sed '/^HEADER/{:1;N;/TAIL/!b1;/DOG/d}' text Just for fun other variants of awk: one: awk ' BEGIN{prn=1} /^HEADER/{block=1} block{ if(/DOG/) prn=0 if(!/^TAIL/){ line=line $0 "\n" next } } prn{print line $0} /^TAIL/{ ...


2

With perl: perl -0777 -lne 'print for grep !/DOG/, /^HEADER.*?TAIL.*?\n/mgs' your-file With awk: awk '! inside {if (/^HEADER/) {inside = 1; skip = 0; content = ""} else next} /DOG/{skip = 1; next} ! skip {content=content $0 "\n"} /^TAIL/ {if (!skip) printf "%s", content; inside = 0}' your-file


1

I was looking for something similar to grep, where it will exit 1 if a match is not found. Here is the equivalent with awk: awk ' BEGIN {z=1} /bravo/ {z=0; print} END {exit z} ' alpha.txt Example


1

Just for fun: sed sed -n '1h 3{ x G s/v1\n\S* \(\S* \S*\).*/\1/p t s/.*\n\(\S*\) \S* \S*\( \S*\).*/\1\2/p } 4Q' file test, head, tail, cut [ $(head -1 file) == 'v1' ] && field='2,3' || field='1,4' head -5 file | tail -1 | cut -d" " -f"$field" bash ...


2

You can try this one: awk 'NR == 1 { if ($1 == "v1") { p = 1; } } NR == 3 { if (p) { print $2 "\t" $3; } else { print $1 "\t" $4; } }' file


0

I am not familiar with snmpwalk but it seems from the question (and from my former invalid answer) that it produces several lines of output. It also seems to me like most of the result of your script is repetitive/redundant, i.e. there are always two lines with the format: A,B,C ... A,B,D and you want another ouput with half the lines and looking like ...


0

A simple route forward is to put the output of each invocation of snmpwalk into a variable and then concatenate them with a little awk filtering, something like: #!/bin/sh DBFILE=$(cat /tmp/routers.all | awk -F: '{print $1}'| grep access) for OUTPUT in $DBFILE do FIRST_HALF=`/usr/bin/snmpwalk -Os -c pass -v 2c $OUTPUT iso.0.8802.1.1.2.1.4.1.1.8.0 | tr ...


0

Please don't use awk for XML parsing. This is a bad idea, because XML supports things like line feeding, indentation, line wrapping on attributes and unary tags - all of which means that semantically identical XML breaks when you use a line/field/regex oriented approach. So I would strongly suggest using an XML tool to build your XML - and as an example: ...


0

I see there is no answer, so my suggestion sort -k2,2 -nk3,3 file


2

If you so like awk awk ' FILENAME != ARGV[3] { m[$2,$3] = 1 next } !(($2,$3) in m) ' file3.txt file2.txt file1.txt > out.txt As for me much easy cut -d" " -f 2,3 file2.txt file3.txt | grep -v -f - file1.txt > out.txt


1

Possible solution with awk: awk 'FILENAME == ARGV[1] { m[$2,$3] = $0; next; } FILENAME == ARGV[2] { if (!(($2,$3) in m)) { m[$2,$3] = $0; } next; } { if (!(($2,$3) in m)) { print $0 >"out.txt"; } }' file3.txt file2.txt file1.txt First we read the first file and create array with keys column 2 and 3. Then we ...


2

These are effectively multi-line records separated by a blank line. Awk is great for handling this kind of data: pactl list sink-inputs | awk -v RS="" '/VLC/' If you want to be really nit-picky about not including the bottom part of the record after the first occurrence of "VLC", then: pactl list sink-inputs | awk -v RS="" -v FS="\n" '/VLC/{ for(i=1; ...


3

Perl one-liner: perl -pe's/\[gene=([^\]]*)\K\]/$h{$1}++?"$h{$1}]":"]"/e' yourfile Explained: -p: execute the code for each line of the file (stored in $_) and print $_ at the end. -e: code. s/regex/replacement/e: Match regex and replace it with replacement on $_. The regex: /\[gene= # match [gene= ([^\]]*) # match anything but "]" and put it a ...


5

With ed: ed -s <<'IN' r !pactl list sink-inputs /VLC/+,$d ?Sink Input?,.p q IN It reads the command output into the text buffer, deletes everything after the first line matching VLC and then prints from the previous line matching Sink Input up to current line. With sed: pactl list sink-inputs | sed -n 'H;/Sink Input/h;/VLC/{x;p;q}' It appends ...


5

I'd use Perl's paragraph mode: pactl list sink-inputs | perl -00ne 'print if s/(.*?VLC.*?\n).*/$1/ms' The -00 sets the input record separator to \n\n so a "line" is a paragraph. Then, the substitution will match everything until the first VLC and then anything until the 1st newline and save them as $1. Everything after that is removed (since we're ...


0

Just to be clear, are you using the "old awk" (/usr/bin/awk/) or the "new awk" (/usr/xpg6/bin/awk)? An online man page reference and GNU Awk's help page specifies the distinction quite clearly. Assuming if you are referring to "new awk" where you can use variable assignments via -v, you can also consider this: $ awk -v RS='name-' -v OFS='\t' 'NR>1{print ...


0

Try: $ awk -F\' -vOFS=\' '{$4=FNR}1' file INSERT INTO db1 VALUES ('2', '1', '3', '1975', '3', '5', '1'); INSERT INTO db1 VALUES ('4', '2', '1', '1987', '3', '1', '1'); INSERT INTO db1 VALUES ('2', '3', '3', '1975', '3', '5', '1'); INSERT INTO db1 VALUES ('4', '4', '1', '1987', '3', '1', '1');


2

What about awk -F\' '{print $2}' Just escape the ' Getting the output field separator back takes a bit more quoting, but can be done by setting OFS inside awk. Putting it all together with your question it would look like (with much appreciated cleanup from @Peter.O): awk -F\' -vOFS=\' '{$2++;print}'


3

Here's another awk way (which, I now see, is just an uglier version of @Costas's): $ awk -F'[- ]' '($1~/name/){k=$2}($1~/school/){print k,$NF}' file JOHN NY TOM TX LILLY LA ROSY WA You can also use grep: $ grep -oP '^(name-\K\S+|school.*\s+\K.*)' file | paste - - JOHN NY TOM TX LILLY LA ROSY WA In your particular example, of course, you ...


5

By GNU sed sed -n '/^name-/{s///;N;s/[a-z].*\s//p}' file JOHN NY TOM TX LILLY LA ROSY WA By GNU awk awk -F'[ -]+' '/name/{a=$2}/state/{print a,$3}' OFS='\t' file JOHN NY TOM TX LILLY LA ROSY WA By grep grep -o '[[:upper:]]\{2,\}' file | paste - - JOHN NY TOM TX LILLY LA ROSY WA


2

awk '/name/ {gsub(/name-/,""); printf "%s\t",$1} /school/ {print $3}' file JOHN NY TOM TX LILLY LA ROSY WA


2

Possible solution with awk (I will edit if needed because it is a little unclear from your question what are exact requirements): awk 'FILENAME == ARGV[1] { m[$2,$3] = 0; z[$2,$3] = $5; next; } FILENAME == ARGV[2] { if (($2,$3) in m) { m[$2,$3] = 1; z[$2,$3] = $5 " " z[$2,$3]; } next; } { if (($2,$3) in m && ...


3

To select shared lines in all files you can use grep (out.txt) grep -ho ' [0-9] [a-z] ' file3 | grep -Fof - file2 | grep -Ff - file1 select operatable field (as variant cut -d' ' -f3,4 file3) and use it to search it in next file2 and file1. As usual for joining 2 file use join command (surprise!) (out2.txt) join -j 3 <(sort -k3,4 file1 | sed 's/ ...


0

If you don't need the email to be in a particular field of the .csv file, and only need to print those lines of the file that contain one of the target emails, you could just use grep: grep -wFf emails.txt file.csv > newfile.csv If they do need to be in a specific field, use @ChrisDown's solution. Note, however, that csv files can be quite complex. For ...


1

Assuming that the e-mails in X.txt are one per line, you can build an array, check it for existence, and then print out the matching lines: $ awk -F: 'FNR==NR { a[$2] = $0; next } ($1 in a) { print a[$1] }' customers.csv X.txt Foo Bar:foo@bar.com Baz Qux:baz@qux.com FNR==NR will only be true when in the first file. Here are the files I used to test this: ...


3

You might want to check this site about diff3 with this program you can compare 3 files as sample output: $ diff3 parent.txt your.txt mine.txt ==== 1:1,2c Hello, This is parent file. 2:1,2c Hello, This is your file. 3:1,2c Hello, This is my file. You can use diff3 file1.txt file2.txt file3.txt > output.txt


0

Simply use awk. It's good for such tasks as selecting data based on columns: echo '3:ââsdb1 8:17 1 984M 0 part /media/(NAMEOFDRIVEHERE)' | awk '{print $7}'


0

I wrote the following script , It read dates.txt and convert all of date to EPOC(timstamp) and print them, Of course i wrote with bash , You need to change name of file and pipe output to a newlogfile: #!/bin/bash declare month=(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); while IFS= read -r line <&3; do sec1=`echo $line |awk -F/ '{print ...


0

Another way is using grep: grep -o -E '(/.*)$'


2

Using sed: sed -n '/media/{s#[^/]*/#/#;p;}' or awk: awk '/media/{print $NF}' or using lsblk itself: lsblk -nlo mountpoint | grep 'media'


0

Try: awk 'FNR==2,FNR==5{$7=$9=60}1' input.dat > tmp


0

Modify your script on this way: awk '{if (NR>=2&&NR<=5) $7=60; print}' input.dat > tmp && mv tmp input.dat The problem you observe in your variant is because w/o explicit print clause awk will print the input line


2

With awk you can use print: awk '{ print "I am", $1, "of earth;" }' list or printf: awk '{ printf("I am %s of earth;\n", $1); }' list


2

Using sed: $ sed 's/.*/I am & of earth;/' file.txt I am john of earth; I am paul of earth; I am rose of earth; I am lily of earth;


3

As I interpret your question, you're trying to get a simple awk script to run, and then you'll elaborate a ksh script around it. Towards that end, you'll need to modify your script like this: 1 #!/bin/ksh 2 awk 'BEGIN { print "START" } \ 3 { print "Hello World" } \ 4 END { print "STOP" }' 5 6 7 exit 0 Your script will execute awk ...


3

Use awk: awk '{a[NR]=$0} {if(NF!=1){y=$2;x=$0;b[x]=1;c[x]=1}else{b[x]++;if(y==$1){c[x]++}}} END{for (n in a){z=a[n];print z" "b[z]" "c[z]}}' file Admittedly it isn't the simplest one, but it works for me with your given input. Explanation: {a[NR]=$0}: First load all content of the file in an array called a. if(NF!=1): If it's a row where we have ...


0

One can use power of vim: :g/part of duplicate string/d Very easy. If you have couple more files (such as gzipped rotated logs), vim will open them without any preliminary uncompression on your side and you can repeat the last command by pressing : and ↑. Just like repeating last command in terminal.


0

The below sort/awk tandem does it: sort -n | awk '{a[i++]=$0;s+=$0}END{print a[0],a[i-1],(a[int(i/2)]+a[int((i-1)/2)])/2,s/i}' (it calculates median as mean of the two central values if value count is even)


2

Here is one approach: awk 'BEGIN{FS="\t"} (($5!=0)+($6!=0)+($7!=0)+($8!=0))>=2' And a terser variant for the C hackers: awk -F $'\t' '(!!$5+!!$6+!!$7+!!$8)>=2'


1

Try this: for i in ./*.mcp; do if [ -f "$i" ]; then tail -1 "$i" fi done | awk '{ print NR, $8, $9, $10 }'


1

With GNU awk (version 4.x) try this: awk 'ENDFILE { printf "%d %s %s %s\n", ++c, $8, $9, $10}' *mcp > "${Pout}${output}" echo "Finished Looping through each file." With other awks and shells like bash try: for f in *mcp do awk -v c="$((++c))" 'END { printf "%d %s %s %s\n", c, $8, $9, $10}' "$f" done > "${Pout}${output}" echo "Finished Looping ...



Top 50 recent answers are included