Hot answers tagged

4

It's quite simple with sed: sed -e '/\.mtt\.corp$/!s/$/.mtt.corp/' <file That does substitute end of line with .mtt.corp on each line which does not end with that string.


3

Using GNU awk, which has arrays of arrays (may require gawk version 4) gawk ' NF { n[$1][$2] += $3 } END { for (name in n) { print name ":" for (fruit in n[name]) printf "%16s %2d\n", fruit, n[name][fruit] } } '


3

You could split the 2nd field on : and if you get more than 2 pieces (that is, the number of elements in array z) keep only the 1st one: awk '{n=split($2, z, ":");if (n > 2) $2=z[1]};1' infile If you wanted to use sub you could do something like: awk '{sub(/:.*:.*/,"",$2)};1' infile that is, attempt to replace two colons (or more).


3

Let's decompose both commands num_rec=`cat test.csv |wc -l|tr -d " " | nawk '{printf("%0.6d\n", $1) }'` is a compounded command : A | B | C | D A: cat test.csv just cats the file test.csv (and is also a UUOC, "Useless Use Of Cat") B: wc -l will count the number of lines from its input (stdin), which is here test.csv's content. So it will count the ...


3

Limit the * to non-spaces: sed -i 's/-agentpath[^ ]*//' files


2

awk can certainly do float comparisons if called from your shell script. num1=0.502E-01 num2=0.01 awk -v a="$num1" -v b="$num2" 'BEGIN{print(a>b)}' 1 awk -v a="$num1" -v b="$num2" 'BEGIN{print(b>a)}' 0


2

Try this: awk '/^[*][*][*] /{ if ($0 in seen) fname=$0; else seen[$0];} fname{print>fname}' file How it works awk implicitly reads a file line by line. For each line read, we do the following: /^[*][*][*] /{ if ($0 in seen) fname=$0; else seen[$0];} For any line that begins with three stars and a space, we check to see if we have seen that line ...


1

awk '$3 !~ /\./ { sub($3, $3 ".00") } { print }' columns This suggestion is based on the sample input, obviously. It works as follows: if column three (the code in the OP uses column nine, but the sample input isn't that long) does not contain a literal dot, replace column three with column three plus the literal string .00 (in other words: append the ...


1

To pass strings to an awk script, pass them through environment variables. export VAR VAR1 awk ' 1 $0 == ENVIRON["VAR"] {print ENVIRON["VAR1"]} ' ~/Scripts/tmp/file.txt I reorganized your script to make the logic simpler. I also replaced the regexp matching by a string comparison: with regexp matching, $VAR would be treated as a regular ...


1

If you don't mind having the output sorted, you can keep track of the current person and the current fruit, and sum until either changes: #!/usr/bin/awk -f NF { if (who != $1) { if (count > 0) { printf "%16s %2d\n", fruit, count } who = $1 printf "%s:\n", who fruit = "" count = 0 } ...


1

Here is one bash solution as well : declare -a a=(1999 2000 2001) url='/test/test' for i in "${a[@]}" ; do echo "$url" | sed "s:/:/$i/:2"; done


1

You specify only one "URL" the following Python program support a list of them: urls = [ '/test/test', ] years = [1999, 2000, 2001] for url in urls: for year in years: spliturl = url.split('/') spliturl.insert(2, str(year)) print('/'.join(spliturl)) The trick is to insert the year at the second position, the first ...


1

Instead of printing matching lines with decorations, decorate matching lines, and print everything: awk '/User@Host:/ { $0 = "\033[32m" $0 "\033[39m" }; /Query_time:/ { $0 = "\033[29m" $0 "\033[39m" }; 1' This awk program consists of three patterns and associated actions: lines matching User@Host: are processed with $0 = "\033[32m" $0 "\033[39m", which ...


1

A perl solution: $ perl -lpe '$_ .= ".mtt.corp" if !/\.mtt\.corp$/' file linuxA.mtt.corp linux3V.mtt.corp linux4B.mtt.corp linux2A.mtt.corp linux5v.mtt.corp The -p makes perl print each line of the input file after applying the script given by -e and the -l removes trailing newlines from each input line and adds one to each print call. The $_ variable is ...


1

How about awk ' BEGIN {FS="\n"; RS="\n\n+"} {for (i=1;i<=NF;i++) a[i] = a[i] == ""? $i : a[i]"\t"$i; next} END {for (i in a) print a[i]} ' file.ex Testing: awk ' > BEGIN {FS="\n"; RS="\n\n+"} > {for (i=1;i<=NF;i++) a[i] = a[i] == ""? $i : a[i]"\t"$i; next} > END {for (i in a) print a[i]} > ' file.ex efifc1a nhdw4s jfhg ...


1

If you accept having temporary files, you could do it as a two step process with awk and paste: n=$(awk '{ print $0 > NR; close(NR) } END { print NR }' RS= file.ex) paste $(seq $n) Or as a one-liner: paste $(seq $(awk '{ print $0 > NR; close(NR) } END { print NR }' RS= file.ex)) Output in both cases: efifc1a nhdw4s jfhg hygg4a wesf3a gsfar ...


1

Here's a fairly simple and straight-forward shell script that uses jsonpipe to do what you want. It doesn't use any fancy sh/bash features, and does only the bare minimum sanity checking of filenames. NOTE: jq is far more capable than jsonpipe, but jsonpipe is simpler and easier to use when you don't particularly care (or want to know) about the structure ...


1

I like joseph's answer but needed it to strip // comments also so I modified it slightly & tested on redhat # no comments alias alias nocom="sed -E '/^[[:blank:]]*(\/\/|#)/d;s/#.*//' | strings" # example cat SomeFile | nocom | less I bet there's a better way to remove blank lines than using strings but it was the quick & dirty solution I used. ...



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