[Solved] split a line and print in second line [closed]

with open(‘path/to/input’) as infile: for line in infile: if line.startswith(“@”): line = line.strip() print(line) print(line.rsplit(“#”, 1)[1]) elif any(line.startswith(e) for e in “#+”): print(line.strip()) solved split a line and print in second line [closed]

[Solved] awk printf with variable

Note that foo=4/3 sets foo to the string 4/3. When that is printed via %f, ‘4/3’ is treated as 4; when that is printed with %s, it is printed as 4/3. If you want to evaluate the expression, you need it evaluated inside the script. For example: awk ‘END {printf “%f\n”, foonum/fooden }’ foonum=4 fooden=3 … Read more

[Solved] Return not so similar codes from a single group [closed]

I would harness GNU AWK for this task following way, let file.txt content be 9003103 9003103 9003978 9003979 9003763 9003728 9003543 9003543 9003543 then awk ‘BEGIN{RS=””}{diff=$NF-$1;diff=diff>0?diff:-diff}diff>NF’ file.txt gives output 9003763 9003728 Explanation: I set RS to empty string to provoke paragraph mode, thus every block is treated as single line, then for each block I … Read more

[Solved] Splitting comma set files

This might work for you (GNU sed): sed -r ‘s/^(.*\|.*\|)([^,]*),([^|]*)(\|.*\|.*\|)([^,]*),([^|]*)(.*)/\1\2\4\5\7\n\1\3\4\6\7/;P;D’ file Iteratively split the current line into pieces, building two lines separated by a newline. The first line contains the head of the 3rd and 6th fields, the second line contain the tails of the 3rd and 6th lines. Print then delete the first of … Read more

[Solved] sort a field in ascending order and delete the first and last number [closed]

Python: with open(‘the_file.txt’, ‘r’) as fin, open(‘result.txt’, ‘w’) as fout: for line in fin: f0, f1 = line.split() fout.write(‘%s\t%s\n’ % (f0, ‘,’.join(sorted(f1.split(‘,’), key=int)[1:-1]))) The body of the loop can be unpacked as: f0, f1 = line.split() # split fields on whitespace items = f1.split(‘,’) # split second field on commas items = sorted(items, key=int) # … Read more

[Solved] Error in shell script and how to write to a file [closed]

I think this question is fine now, the input file is good enough after edit, I can fully understand what you ask for now. With awk, you need learn to use 2-d array, it will simplify the code. awk ‘BEGIN{print “Instance id Name Owner Cost.centre”} /TAG/{split($0,a,FS);a[4]=tolower(a[4]);$1=$2=$3=$4=””;b[a[3],a[4]]=$0;c[a[3]]} END{for (i in c) printf “%-18s%-26s%-14s%-20s\n”,i,b[i,”name”],b[i,”owner”],b[i,”cost.center”]}’ file Instance id … Read more

[Solved] Remove lines from a text file that contains multiple string patterns from another file in linux [closed]

$ cat tst.awk BEGIN { FS = “[\t|]” } NR==FNR { for (i=1; i<=3; i++) { if ($i == “”) { $i = “N.A.” } } a[$1 OFS $2 OFS $3] next } !(($3 OFS $5 OFS $6) in a) $ awk -f tst.awk file1 file2 123|234|aa|ur29842|b|c|234|567 123|234|aa|ur2909842|bb|ccc|234|567 123|234|aaa|ur29042842|bb|cc|234|567 123|234|N.A.|ur2922|bbb|cccc|234|567 0 solved Remove lines from … Read more

[Solved] Removing Leading Zeros within awk file [duplicate]

Try this: sub(/^0+/, “”, $2)? $ awk ‘BEGIN{b=”000000000000787301″; sub(/^0+/, “”, b); print b;}’ 787301 or typecast it by adding zero: $ awk ‘BEGIN{b=”000000000000787301″; print b+0;}’ 787301 update A working example based on comments: $ echo ‘E2EDP19001 02002000000000000797578’ | awk ‘{$2 = substr($2,6); sub(/^0+/, “”, $2); print $2;}’ 797578 or, preserve $2: $ echo ‘E2EDP19001 02002000000000000797578’ … Read more