Hot answers tagged

10

code.awk: BEGIN{j=1} NR==FNR{a[NR]=$0;next} sub(/=> '.*',$/,"=> '"a[j]"',"){++j} 1 awk -f code.awk file2 file1 > file3 Line by line explanation: Initialize j=1. Put each line of file2 in the array a. In file1, for each line, try to substitute a string matching the => '.*',$ regex by the concatenation of => ' a[j] ',. If ...


6

Here’s a generic solution using AWK, adapting to the number of common components: BEGIN { FS = "-" } NF == 2 { split($1, start, /\./) split($2, end, /\./) printf $1 for (i = 1; i <= length(start); i++) { if (start[i] != end[i]) { for (j = i; j <= length(end); j++) printf "%s%s"...


6

Past all that quoting hell, what you end up running is awk '{print }'. That's because in the alias assignment \$2 is in a double-quoted string, where the backslash prevents expanding $2, but when the alias is used, the $2 is left unquoted and expands to whatever the current positional parameter $2 is, probably empty in your interactive shell. The print ...


6

POSIXly, you could do: awk -F '] ' '{ out = $1 for (i = 2; i <= NF; i++) out = out FS toupper(substr($i, 1, 1)) substr($i, 2) print out}' < infile Or: awk ' BEGIN {FS = OFS = "] "} { for (i = 2; i <= NF; i++) $i = toupper(substr($i, 1, 1)) substr($i, 2) print }' < infile That defines "] " as the ...


5

Awk scripts consist of pattern {action} pairs. if pattern is empty, then {action} will be applied to all records if {action} is empty, then the default action {print} will be applied to all records matching pattern awk '$1' will apply the default action {print} when pattern $1 evaluates true. Since a non-empty string is true, it will print the whole of any ...


5

You're not printing. Try awk '{print $1}' input.txt > output.txt When you just give an expression (the way you tried), awk works somewhat like default grep: completely print any matching lines: awk '/regexp/' file.txt - print lines matching regexp awk 'NR==3' file.txt - print line 3 awk '1' file.txt - print all lines where 1 is true, i.e. all (okay, an ...


4

An option with awk: awk -F'[.-]' '{ print $1"."$2"."$3"."$4"-"$8 }' Output: 10.1.1.1-3 10.100.100.11-31


4

Here's a sed approach. It will look for everything up to the last - on the line, save that as \1, then save the last occurrence of a . followed by one or more digits as \2. The entire line is then replaced by \1\2: $ sed -E 's/(.*-).*\.([0-9]+)/\1\2/' file 10.1.1.1-3 10.100.100.11-31 Alternatively, in awk, you could set the input field separator to . or -, ...


4

Another awk approach, using match and substr: $ awk -v pat="'[^']*'" -v q="'" -v file2='File2' ' BEGIN{OFS=FS=" => "} match($2,pat) && ((getline str < file2) > 0) { $2 = substr($2,1,RSTART-1) q str q substr($2,RSTART+RLENGTH) } 1 ' File1 'text_clear' => 'emptied', 'text_search' => 'lost'...


4

If on a GNU system, you can use sed: sed 's/] ./\U&/g' <infile stream editor 'substitute/replace-what/replace-with/globally' <inputfile Here "replace-what" is a literal ] followed by a single space then a single character (. matches a single character except \newline but would match a \newline character if it was ever found in the ...


3

Another awk solution: $ awk -F"'" -v OFS="'" '(getline line < "file2")==1{$4=line} 1' file1 'text_clear' => 'emptied', 'text_search' => 'lost', 'text_enabled' => 'turned off', This relies on the fact that each input line has 2 single quote characters before the field to be replaced. If you want to ...


3

You're mixing up shell and awk syntax here. sh (or bash) and awk are two interpreters for two different languages that have nothing to do with each other. The syntax of awk is very C-like. In awk, variables are referenced with var, not $var like in shells, and $ in awk is a unary operator to retrieve the value of an input field (or the full record if applied ...


3

Capture the first digit, replace it with | and the captured digit: sed -e 's/\([0-9]\)/| \1/'


3

If you want to extract the lines that has the smallest value in the 11th column grouped by gene, then use the gene identifier as the key in an associative array in awk to keep track of the smallest value for that gene: $ awk '{ gene = $1 } min[gene] == "" || min[gene] > $11 { min[gene] = $11; line[gene] = $0 } END { for (gene in line) print line[...


2

awk -F "+" 'NR%2{nf=NF;l=$0;next}{sep=(nf==NF?" + ":RS);printf "%s%s%s\n",l,sep,$0}' file For a line by line analysis, let's unwind it: awk -F "+" ' NR%2{nf=NF;l=$0;next} { sep=(nf==NF?" + ":RS) printf "%s%s%s\n",l,sep,$0 } ' file Set field separator to +. If the line number is odd (...


2

While this is an awk question, I'd nevertheless throw cut in here as good alternative for this specific task: cut -d' ' -f1 input.txt > output.txt The default delimiter (tab) has been replaced with space by -d' ' and the first field selected by -f1.


2

The file you show doesn't look like it is comma separated, so you don't need (or want) -F,. It looks like all you want is this: cd folder_with_all_logs awk '{print $2,FILENAME}' * > final_summary.log


2

This seems like an odd requirement, but to extract digits from arbitrary filenames, you could use parameter expansion to remove anything from the filename that's not a digit: for f in * do printf '%s\n' "${f//[![:digit:]]/}" done


1

With awk: awk '$4=="ccc"{ $6="end" }1' file The 1 at the end just prints the current line and is a shortcut for { print } (see what is the meaning of 1 at the end of awk script).


1

You could use an if test. For example: awk -F, ' BEGIN { OFS="," } { if ($11<=0) { $12="NR" } else { $12=log($11) } ; print } ' This will set field 12 to the string "NR" or the the log of field 11, depending on the value seen. With your input file the output now looks like: QUEENS_DEC,5/26/2002,3.06, ,16.61342593,0....


1

First, you can set FS: -v FS="[]][[:space:]]+" i.e. []] followed by [[:space:]]+. With this you can get the field for making changes: $ awk -v FS="[]][[:space:]]+" '{print $2}' file john is bla bla blue house in Chicago... accessing the safe... bla bla Here you "want to convert the first letter found after the space to uppercase&...


1

$ cat tst.awk BEGIN { FS="[[:space:];]+" } { rec[++nf] = $1 } $1 == "Out" { printf "%s(", rec[1] for (i=2; i<=nf; i+=2) { printf "%s%s", (i>2 ? "," : ""), rec[i] } print ")" for (i=2; i<=nf; i+=2) { print rec[i+1], rec[i] ";" } ...


1

LC_ALL=C sed 's/^\([0-9.]*\.\)\([0-9]\{1,\}-\)\1\([0-9]\{1,\}\)$/\1\2\3/' Would give the expected output for lines that are in the anything.digits-anything.digits and leave the lines that don't match that pattern (like 1.2.3.4-1.2.6.254) alone.


1

Your data appears to be in comma-separated value (CSV) format. As long as your CSV file is "simple", i.e. none of the comma-separated fields contains strings with commas, and the name is always in field 4 as implied by your example input, the following awk script which uses the , as field separator would do: awk -F',' '{print $4}' file.txt If the ...


1

awk '{ print length(), $0 | "sort -n" }' file.txt | tail -1 Reference: https://www.systutorials.com/how-to-sort-lines-by-length-in-linux/


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