Hot answers tagged

32

Variables are referenced by name as in var, not $var in awk. $n refers to the nth field: $1 for the first field, $2 for the second... or the whole record for n == 0 ($0 is the full record). Those don't have to be literal numbers. You can use $(1+1) or $variable. If variable contains 1, then $variable will be the first field. A commonly used one is $NF for ...


3

awk is good for this, but you could do it simpler, with calcc() { awk "{\$$1=$2;print}" | column -t } Whether that's better or not is your choice.


2

You can accomplish this with awk awk '{sub(/".*",/,"");print}' filename


2

With sed: echo 00000012- | sed -e 's/\([0-9]+\)\(-\)\?/\2\1/' gives -00000012. That is, matching two groups, the first with digits and the second with a sign, but using zero-or-more matches of that, and then replacing the two interchanged.


2

I think this does what you want; it accepts an awk variable named "factor" that is can easily be set to whatever you want: awk -v factor=8.06573 '{printf "%2.9f %2.9f\n", $1 * factor, $2 * factor}' With the given input, it outputs: 34.193855762 35.948152037 34.220472671 33.078365303 34.585043667 33.260650801 33.961562738 36.169959612 34.176917729 33....


2

awk '!x[$0]++' <<< "$list" | while read -r line; do array[count++]=$line done The array (italic) in this case is a part of the subshell (bold). The $line and $array has a value whilst the subshell is alive, so to speak. Once the subshell finishes, aka dies, the parent (spawner) environment is restored. This includes obliteration of any ...


2

I would use awk awk --posix '{ gsub(/\.[[:digit:]]{6}/, "", $1); print }' filename Will target the first field (space delimited) and search for a . followed by 6 numbers and empty it out.


1

With awk probably the simplest way is to do a regular expression substitution on the first whitespace-separated field, replacing everything from the period to the end of the field: awk '{sub(/\..*/,"",$1)}1' somefile


1

awk '{ if (/ID: /) { if (/searchForThisMethod/) { ++part print >sprintf("part-%03d.txt", part) } } }' test.txt Checks if the line matches pattern header-line then does a second check for secondary-pattern before incrementing and writing to the file. Given this is in test.txt ID: searchForThisMethod 1 ...


1

Sort and join (but no awk) method: sort -k2 main.txt > main_sort.txt ; \ sort -k1 changes.txt > changes_sort.txt ; \ { join -i -1 2 -2 1 -o 1.1 2.2 main_sort.txt changes_sort.txt ; \ join -v 1 -i -1 2 -2 1 -o 1.1 1.2 main_sort.txt changes_sort.txt ; } | \ sort -g | column -t Output: word_1 root_A word_2 root_A word_3 genre_B ...


1

You mentioned the join command, but in this case it's not easy to use it. At least not by itself. Let's try to join the two files. To do that, the files first need to be sorted on the join field. We are going to be using the second field from the first file and the first field (which is the default for join) from the second file: $ sort -k2 -o main.txt ...


1

Awk command: awk 'NR==FNR {a[$1]=$2;next} {if ($2 in a) print $1,a[$2]; else print $1,$2}' changes.txt main.txt Output: word_1 root_A word_2 root_A word_3 genre_B word_4 root_C word_5 root_A


1

If your system's grep supports PCRE, you could maybe do $ echo 'aaa string1 bbb aaa string2 bbb aaa string3 bbb' | grep -oP '(?<=(aaa|bbb) )\w*?(?= (aaa|bbb))' string1 string2 string3 or if you need to handle more general amounts of surrounding whitespace $ echo 'aaa string1 bbb aaa string2 bbb aaa string3 bbb' | grep -oP '(aaa|bbb)\s+\K\w*?(?=\s+...


1

I used a combination of grep and sed to accomplish your goal of after \n( and before )\n grep -o '\\n(.*)\\n' test.txt|sed -e 's/\\n//g' Sample output (T.a = 1) (public.cde.newcol = \'013\')


1

You can accomplish this with sed like so: sed -e 's,\\n(.*,,' unset or with awk you have to do a lot of escaping awk -F'\\\\n\\(' '{print $1}' unset to get the right escaping for both the \ before the n and to protect against the special interpretation of the (


1

Some solutions to your problem without the loop # use bash's mapfile with process substitution mapfile -t arr < <( awk '!x[$0]++' <<<"$list" ) # use array assignment syntax (at least bash, ksh, zsh) # of a command-substituted value split at newline only # and (if the data can contain globs) globbing disabled set -f; IFS='\n' arr=( $( awk '!...



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