[Solved] Find rows with the same value in a column in two files

Let’s say your dataset is big in both dimensions – rows and columns. Then you want to use join. To use join, you have to sort your data first. Something along those lines: <File1.txt sort -k2,2 > File1-sorted.txt <File2.txt sort -k3,3 -S1G > File2-sorted.txt join -1 2 -2 3 File1-sorted.txt File2-sorted.txt > matches.txt The sort … Read more

[Solved] How to move everything following a dash to a new column?

You could try this for a input-file called input: csplit -f tempfile input ‘/-/+1’ ‘{*}’; paste tempfile* With csplit we generate one file for each “column” in the desired output (tempfile01, tempfile02, …). Next, we merge these temporary files. With the given sample input, the output of the above command is: jim sally bill jerry … Read more

[Solved] Append text to each of multiple lines in file

Use something simple like sed: sed -r ‘s/^([0-9]*)$/update “table1” set event_type = \’NEW\’ where id=\1/’ file | write back using \1 as placeholder catch digits in the file Previous answer I used an approach based on bash loops, whereas it is a bit overkilling. See a related question: Why is using a shell loop to … Read more

[Solved] “regular expression compile failed (missing operand)” issue in AWK when checking for an asterisk [duplicate]

First of all, * in regex is a quantifier that means “zero or more occurrences”. Since it is a regex metacharacter, it should be escaped, \*: k config get-contexts | awk ‘/\*/{print $5}’ However, since you are looking for a fixed string rather than a pattern you can use index: k config get-contexts | awk … Read more

[Solved] Remove newlines between two words

something like this? kent$ awk ‘/^key.:/{p=1}/^name:/{p=0} {if(p)printf “%s”,$0;else printf “%s%s\n”, (NR==1?””:RS),$0}’ file name: charles key1: howareyou? name: erika key2: I’mfine,thanks name: … handle the spaces: awk ‘/^key.:/{p=1}/^name:/{p=0} {if(p)printf “%s%s”,(/^key.:/?””:” “),$0; else printf “%s%s\n”, (NR==1?””:RS),$0}’ file output: name: charles key1: how are you? name: erika key2: I’m fine, thanks name: … 2 solved Remove newlines between … Read more

[Solved] awk – restricting area in file, printing

The problem of duplicate outputs is due to a single line matching both its own number and the $1 != 26 condition in the end. A simple solution is to add ; next after each prt(…) call. The problem with zero outputs is likewise due to the $1 != 26 matching too much. You could, … Read more

[Solved] awk : parse and write to another file

Use GNU awk for multi-char RS: $ awk -v RS='</record>\n’ ‘{ORS=RT} /<keyword>SEARCH<\/keyword>/’ file <record category=”xyz”> <person ssn=”” e-i=”E”> <title xsi:nil=”true”/> <position xsi:nil=”true”/> <names> <first_name/> <last_name></last_name> <aliases> <alias>CDP</alias> </aliases> <keywords> <keyword xsi:nil=”true”/> <keyword>SEARCH</keyword> </keywords> <external_sources> <uri>http://www.google.com</uri> <detail>SEARCH is present in abc for xyz reason</detail> </external_sources> </details> </record> If you need to search for any of multiple … Read more

[Solved] Why does my Awk command filter my text without a for loop but not when a for loop is added?

I already told you how to approach problems like this. See https://stackoverflow.com/a/42753485/1745001 and just add a line to that answer that says: f[“fechaName”]==5{for (i=1;i<=3;i++) print} Look: $ cat tst.awk BEGIN { FPAT = “([^,]*)|(\”[^\”]+\”)” OFS = “,” } { delete f for (i=1; i<=NF; i++) { split($i,t,/[[:space:]”:]+/) f[t[2]] = t[3] } } f[“fechaName”]==5 { for … Read more