Tag Info

Hot answers tagged

10

You can use sed: sed -i -e 'p' -e 's/o/0/g' file Explanation: -i: activates in-place editing of the file -e 'p': just pastes the line -e 's/o/0/g': replaces o with 0 and pastes the altered line And if you want an awk solution: awk '1;gsub("o", "0")' file >new_file


10

Not near a terminal to test, but how about the oft-overlooked nl command? Something like: cut -f 2 -d , original.csv | nl -w 1 -p -s , > numbered.csv


8

sed -n '/foo/,/goo/p;/goo/q' <bigfile That would print only those lines. If you wanted the line numbers you'd add an =. sed -n '/foo/=;/goo/=;//q' <bigfile The q is important because it quits the input when it is called - else sed will continue to read the infile through to the end. If you don't want to print foo/goo lines you can do instead: ...


6

A sed approach: sed -n '/[^,]*,[^,]*,[^, ]\+/p' file Or a grep solution: grep -oE '^[^,]*,[^,]*,[^, ]+' file And awk: awk -F, '$3 ~ /[^, ]+/' file


6

chaos' answer is good if every line matches. But if some lines don't contain a o, they'll be duplicated. For example: $ echo -e 'foo\nbar' | sed -e 'p' -e 's/o/0/g' foo f00 bar bar   Here are some other solutions which don't duplicate lines: $ echo -e 'foo\nbar' | sed -e '/o/ p; s//0/g' foo f00 bar   $ echo -e 'foo\nbar' | perl -pe '/o/ ...


6

This is not answering your question, but it solves the problem you are trying to solve, in a completely different way: The full command is this (see below for example output): ps -o comm,%cpu,%mem --sort -%cpu -A | head -6 I will describe the parts of it: using ps to have more control about the output Printing only the three columns we need with -o ...


5

If you are okay with abandoning your current approach of using something in subshells to get the line numbers and allowing another utility to print the file, this can be accomplished in pure awk with little difficulty: If you wish to print the lines between foo: and goo: and not the lines themselves, then you can use the following (picked up from here ...


5

top command | awk 'BEGIN { print header; count=5} NR>=8 { if ($0 ~ /your top regex/) next; print fields; if (--count == 0) exit}' On every line starting with #8, if it matches top, ignore it. Otherwise, print the portion of it that you want. The fifth time you print a line (that doesn’t match top), exit.


5

If you're not stuck on sed or awk, use paste echo "R1 R2 R3 R4 R5 R6 R7 R1 R2 R3 R4 R5 R6 R7" | paste -d, - - - - - - - R1,R2,R3,R4,R5,R6,R7 R1,R2,R3,R4,R5,R6,R7 There's no difficulty with your 2nd sample, just have to quote the semicolon so the shell doesn't see it: paste -d ';' - - - - - - - <filename 00000;ND00000056888;Doe, Jane J;F;99 ...


5

Try: $ awk 'FNR==NR{a[$1]=$2;next}{$1 = $1"\t"a[$1]}1' OFS='\t' file1 file2 1 today alot 1 today sometimes 2 tomorrow at work 2 tomorrow at home 2 tomorrow sometimes 3 red new


5

Here are a few approaches, but none will approach the speed of the cut and nl solution above: awk awk -F, '{$1=NR;print $1","$2;}' file.csv > newfile.csv Perl perl -pe 's/[^,]+/$./' file.csv > newfile.csv or perl -F, -ane '$F[0]=$.; print join ",", @F' file.csv Shell (but I don't recommend it for a 200G file, it will take ages) i=1; while ...


4

{ tr -cs '0-9ABC' '[\n*]' | nl -w1 -s: | sort -t: -uk2,2 | sort -t: -k1,1n | sed 's/[^:]*://;/^[ABC]/!{H;$!d };x;y/\n/,/;s/,/\t/' } <<\FILE A 1,2,3,4 B 4,5,6,3 C 2,15 FILE Basically this just takes some measures to ensure that the data can be run through sort -u and still come back again in relatively the ...


4

Simply paste: paste -d'\n' file1.txt file2.txt If you want to use awk, you can: awk '{getline a < "file2.txt";printf "%s\n%s\n", $0, a}' file1.txt


4

Another awk: awk '{$1=RS $1 ORS}NR>1' FS='\n' RS=\> OFS= file


4

try awk '{printf "%s%s",$0,NR%7?",":"\n" ; }' the big part NR%7?",":"\n" : if-then-else : if (NR%7) : NR, (Number of Record) % (modulo) 7 (divide by 7 != 0 ) then printf a ',' else new line. $0 is the whole line


4

Do you specifically require an awk solution?  join file1 file2 > file3 will do exactly what you want.


3

Just in the shell: IPS='192.9.200.12-14 172.17.200.12-89 12.21.1.10-25 127.0.0.1-127' echo $(IFS=\ .; printf %s.%s.%s,%s\ $IPS) OUTPUT 192.9.200,12-14 172.17.200,12-89 12.21.1,10-25 127.0.0,1-127 But a better way - one in which you can actually work with an array - is to turn it into a function: iprng() (IFS=.;for ip do printf %s.%s.%s,%s\\n $ip; ...


3

Match everything up to the last dot and remember it in a group: $ echo $IPS | sed 's/\([^ ]*\)\./\1,/g' 192.9.200,12-14 172.17.200,12-89 12.21.1,10-25 127.0.0,1-127


3

Try: $ awk -F, '$3 && $3 != " "' file XYZ,xyz,xyzs ABC,abc,abcs PQR,pqr,pqrs or: $ awk -F, '$3 ~ /[^[:blank:]]/' file


3

Alternative sed one: sed '/foo/,$!d;/goo/q'


3

Another sed solution: sed -ne 'p;s/o/0/pg' file $ printf '%s\n%s\n' 'foo' 'bar' | sed -ne 'p;s/o/0/pg' foo f00 bar Explanation -n option causes sed suppress the default output. p command write the pattern space to standard output. s/o/O/pg replaces all occurences (g flag) of o with 0, only write pattern space to standard output if a replacement was ...


3

The direct answer to your question is grep -f /path/to/runlist /path/to/file.xml But I think this is an XY question: without thinking about how to solve the question, what are you trying to do? Given that runlist is a file containing xml filenames, I'd use an xml processing tool (like xmlstarlet) to extract the vcpu: $ cat runlist sample1.xml ...


3

You must escape $ sign: $ sh -c "echo 123 | awk '{print \$1 456}'" 123456 Otherwise, $1 is expanded by current shell.


3

You don't need to use variable ba, try: $ echo $a | awk -F',' '{print '"$b"'}' 15986327,415532694,875121642 With this, $b is expanded by the shell, not by awk. And the rest of awk statement is not affect, because they're enclosed in single quote.


3

The input data is kind of paragraph-oriented, so let's read it as a paragraph instead of line-by-line: awk -v RS="\n=\n" ' /PRIMER_LEFT_NUM_RETURNED=[^0]/ { n = split($0, lines, /\n/) for (i=1; i<=n; i++) { if (lines[i] ~ ...


3

Perhaps a little state machine: tac file | awk '/End of script/ {p=1} p {print} p && /Start of script/ {exit}' | tac


3

I always throw perl at everything :-) perl -ne 'if (/^\s/) { $x.=$_ }else{print $x if $x=~/apollo/; $x=$_} END {print $x if $x=~/apollo/}' file.txt Edit: explanation of the one-liner: -n implies a loop over the input, not printing by default at the end of each loop (-p is the same but does print the line by default). -e specifies an expression, or code ...


3

Nice markup: awk '!/^#/ && NF { split($2,a,"-") printf "%s - - [%s/%s/%s:%s] \"%s %s\" %s %s\n", $1, a[3], a[2], a[1], $3, $4, $5, $6, $7 }' file If your input is just the lines starting with 10.000... this makes it: $ awk '{printf "%s - - [%s:%s] \"%s %s\" %s %s\n", $1, $2, $3, $4, $5, $6, $7}' file 10.000.000.000 - - ...


3

Try: $ awk -F'[:}]' '$(NF-1) >= 1334575352456 && $(NF-1) <= 1334575353456' file {"a":"1", "b":"2", "c":"4", "time":1334575352456} -F'[:}]' set fields delimiter is : or }, so you can access the the time value by accessing second from last field, using $(NF-1). To save ouput, use: $ awk -F'[:}]' '$(NF-1) >= 1334575352456 && ...



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