Tag Info

Hot answers tagged

16

You could do something like: awk '{print NF,$0}' file | sort -nr | cut -d' ' -f 2- Basically use awk to print the number of fields then sort by that number an then remove the number. You could do all that in awk (or perl etc) but you get the idea.


11

There are no built-in functions in standard awk to get a date, but the date can easily be assigned to a variable. awk -F, -v date="$(date +%Y-%m-%d)" '$3>date' or in an awk script BEGIN { str = "date +%Y-%m-%d"; str | getline date; close str; } $3>date gawk, does have built-in time functions, and strftime can be used. gawk -F, ...


8

This works, as indicated by jasonwryan: awk 'BEGIN{print "START"}; {print}; END{print "END"}'


7

This can be done in sed with sed -e $'1i\\\nSTART' -e $'$a\\\nEND' 1i means insert before line 1; $a means append after the last line.  The $'…' syntax is bash-specific.  In other shells, you should be able to do this with: sed -e '1i\Enter START' -e '$a\Enter END'Enter


6

sed 's:\\1\\:\ :g' file you will have to escape the backslashes in your match pattern. In the replacement, that's a backslash followed by a literal newline character. Some sed implementations, like GNU sed also support \n there as an non-standard alternative. Output "evSchema" "UAT" "evSN" "uadb" "evDirsep" "/" "evRootPath" "/work_area/APP_UAT/" ...


6

In recent GNU awk one can use PROCINFO array to define many internal parameters including order in which array elements are printed (controlled by element "sorted_in"). Thus we can built and array indexed with the value of NF" "NR, which elements have value of $0 and print it in desired output, in your case that would be "@ind_num_desc": awk '{a[NF" ...


5

Use this: awk -F, '($2==1000 && $4!="A") || ($2==1001 && $4!="B") || ($2==1002 && $4!="C")' file In the curvy brachets are the 3 conditions; if one of them applies the line will be printed. The conditions inside the brackets are connected with a AND, so both must apply.


5

Perl one-liner: print sort { split(' ',$a) <=> split(' ',$b) } <>; If you want to break ties using alphabetical order: print sort { split(' ',$a) <=> split(' ',$b) or $a cmp $b } <>;


5

With awk: awk ' /^end/ { sub(" ", "", indent) } { print indent, $0 } /^describe/ { indent = indent" " } ' <file


4

Through python. s = '''hello: world foo bar baz bar: baz: bin boop bop fiz bang beep bap: bim bam bop'''.splitlines() for i in sorted(s, key=lambda x: len(x.split()), reverse=True): print(i) or with open('/path/to/the/input/file') as f: m = f.readlines() for i in sorted(m, key=lambda x: len(x.split()), reverse=True): print(i, end="") ...


4

Possible awk solution could be: awk 'BEGIN { RS = ""; } { $1 = $1; } 1' matrices.txt > modified_matrices.txt


4

Another awk: $ awk 'BEGIN{getline l <"file1"};{print $0, l}' file2 1 2 3 12 4 5 6 12 7 8 9 12 BEGIN block was executed first before reading input file. The first line in file1 was retrieve using getline() function, stored in variable l With each line of file2, we print it content $0 along with l, separated by OFS, which is a space by default.


4

Many tools can be handy: -n of grep is exactly what you are looking for. grep -n 'bla' file alternatively awk: awk '/bla/{print NR":"$0}' file alternatively perl: perl -ne 'print $.,":",$_ if /bla/' file alternatively sed: sed '/bla/!d;=' file |sed 'N;s/\n/:/'


3

The long|many terms better put into array: awk -F, 'BEGIN{a[1000]="A";a[1001]="B";a[1002]="C"}$4!=a[$2]' file


3

You could translate all spaces and double quotes to | (and squeeze) then cut from the 2nd character to the end of line: tr -s '[[:blank:]"]' \| <infile | cut -c2-


3

using the getline workaround: awk -F, ' BEGIN { "date +%Y-%m-%d" | getline a } { if ( $3>a ) { print 0 }} ' file


3

For your sample input: $ cat /tmp/data | 2015-08-21 - 10:15 | jones | view | Main.Home | | 172.29.192.106 | | 2015-08-21 - 10:31 | wilson | view | Main.Home | | 172.19.6.107 | | 2015-08-21 - 11:40 | smith | resetpasswd | wilson | Mozilla | 172.19.15.105 | | 2015-08-21 - 11:41 | james | view | Main.ChangePassword | | 172.19.15.102 | | 2015-08-21 - 11:41 ...


3

You concatenate (cat) them together, so for awk it is just one file (or standard input in this case). If you want to have desired result run instead awk -F, '{print FNR}' File1 File2


3

Use this instead: awk -F, '{print FNR}' file1 file2 The FNR variable in awk gives the number of records for each input file. But, when you use cat .. | awk awk reads for the stdin file descriptor, therefore awk sees only 1 "file". Try this to understand better (FILENAME contains the current file being processed): $ awk -F, '{print FILENAME" "FNR}' file1 ...


3

Content of file: username:email@email.com:password:salt user:name:email@email.com:password:salt username:email@email.com:password:sa:lt user:name:email@email.com:password:sa:lt With GNU sed: sed -E "s/(.*):([^:]*@[^:]*):([^:]*):(.*)/\1', '\2', '\3', '\4/" file Output: username', 'email@email.com', 'password', 'salt user:name', 'email@email.com', ...


3

If you're willing to take all the digits of the pid you could use pgrep to do this for you: pgrep -d " " -f ^/usr will print a space separated list of all the PIDs of processes whose command starts with /usr If you really want to use the ps command, awk would be a better tool than sed to do what you want. You could do it, with just the first 5 ...


3

If you're already using sed, you can use 1 to match the first line and $ to match the last line (see Scott's answer). If you're already using awk, you can use a BEGIN block to run code before the first line and an END block to run code after the last line (see Michael Durrant's answer). If all you need to do is add a header and a footer, just use echo and ...


2

With POSIX awk: awk '!(FNR%3==1)' <file With POSIX sed: sed -e '1d;n;n;d' <file With GNU sed: sed -e '1~3d' <file


2

If you like to get the current time_t, this can be done by calling srand() and then call t = srand(). $ awk 'BEGIN{srand(); print srand()}' 1440536144 This works as posix requires the random function to be initialized with the current time if called without arg and to return the previous seed.


2

You can do it by assigning a variable: cat /path/to/file | awk -v date="$(date +'%Y-%m-%d')" -F, '{if($3>date){print $0}}'


2

Using awk $ awk -v n=$(cat file1) '{print $0,n}' file2 1 2 3 12 4 5 6 12 7 8 9 12 On csh/tcsh, try: awk -v n=`cat file1` '{print $0,n}' file2 How it works -v n=$(cat file1) This assigns the contents of file1 to the awk variable n. print $0,n This prints each line followed by n. Using sed $ sed '1{h;d}; G;s/\n/ /' file1 file2 1 2 3 12 4 5 6 12 7 ...


2

You want to change these "string"s which are in lines where there are no # character or this character is after "string", so that you can have comments at the end of the lines: ##################################### # Blah blah blah string blah blah ##################################### PKG_NAME="string" PKG_DESC="string-foo" PKG_A="string" # this is ...


2

In sed you can stop doing actions if a pattern is found with an exclamation mark: sed '/#/!s/string/replacement/' file I.e. for lines matching '#' (at any position) do not do the replacement - else do. In your case result is: ##################################### # Blah blah blah string blah blah ##################################### ...


2

In Vi/Vim you can run simply: :%j to join all the lines together, or: :%v/^$/-1j to join all matrices separated by new line (Join lines between a certain text pattern in Vim). If you need this done from the command line, try either: ex -s +%j +"wq modified_matrices.txt" matrices.txt to join all lines, or: ex -s +'%v/^$/-1j' +'wq! ...


2

This works with mawk: awk 'NR==1{$7="G";print;next} \ $3~/^[A,C,G,T]$/ || $4~/^[A,C,G,T]$/ {$7="P"} \ $3~/^[I,D,R]$/ || $4~/^[I,D,R]$/ {$7="Q"} \ $4~/[A-Z][A-Z]/ || $3~/[A-Z][A-Z]/ {$7="Q"} 1' file line: In the first line write the G in the header. line: If $3 of $4 are A, C, G or T then $7 is P. line: If $3 of $4 are I, D, or R then $7 is Q. line: ...



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