Tag Info

New answers tagged

-1

$ cat file | awk -F"," \ '{for ( x=NF ; x>1 ; x-- ) { if ( $x ~ /^Failed/ ) { x = 0 ; print } ; \ if ( $x ~ /^Successful/ ) x = 0 } }' This worked for me.


0

One way to handle literal single-quotes in awk is to use the equivalent octal escape code \047 e.g. FS exactly equal to '|' printf "foo'|'bar'|'baz\n" | awk 'BEGIN{FS="\047\\|\047";} {print $2}' bar Note that the | needs to be escaped in order not to be treated as a regex logical OR; alternatively you can use a [...] character range instead of escaping ...


0

The awk FS variable is expected to contain a regular expression. Some characters are special within regular expressions and the vertical bar character is one of those characters. Placing it within brackets makes it part of an anonymous character class and vertical bar is not special within such classes. This makes awk treat vertical bar as an ordinary ...


1

You cannot use a for loop like that. Try something like: awk '$2==100{print 1,$1,$1+500; exit}' OFS='\t' file


2

{ 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 ...


1

I don't really know what all this is about, but those were some long lines - it was hard to interpret for me. So, I did this: sed 's/,S[^,]*/,A/g;s/,Fa[^,]*/,F/g ' <<\DATA Name,Mon,Tue,Wed,Thu,Fri,Mon Linux,Successfull Fullbackup; Failed Fullbackup,,,,,Failed Fullbackup Unix,,,,Failed Fullbackup,, Windows,Failed Fullbackup,Failed Fullbackup,Failed ...


0

A couple of variations on Stéphane’s answer: (line; grep $'[\t,]Failed[^\t,]*[\t,]*$') < input The (line; grep …) construct comes in very handy; for example, for filtering the output from ps.  If your system doesn’t have the line command, (read x; echo "$x"; grep $'[\t,]Failed[^\t,]*[\t,]*$') < input should work, too.  Alternatively, grep -E ...


0

It seems that you only want to get information from list of files in runlist. You can: grep <info> -- $(cat runlist)


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 ...


1

If the files are sorted on the join field (field #1) as shown in your example, then you could use the join command join -t, -o 1.1,1.2,2.3,2.4,2.5,2.6,2.7,2.8 file1 file2


2

Try: $ awk -F, -v OFS=, 'NR==FNR{a[$1,$2]=1; next} ($1,$8) in a {print $1,$8,$2,$3,$4,$5,$6,$7}' file1 file2 which gives: 111,123abc,hhh,eeee,rere,,23,2014,123abc 222,234bcd,jjkj,7878,uhjj,1,45,2013,234bcd This isn't exactly the same as your output as you have 8 columns in the first and 9 in the second. I've assumed that's a typo :-)


3

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


1

An awk solution: awk '/^>/{if(FNR>1)printf "\n";print;next}{printf "%s", $0}' file A perl solution, like @choroba's answer, but handling final newline: perl -pe 'chomp unless /^>/ or eof;print "\n" if /^>/ and $. > 1' file


2

perl -pe 'chomp unless /^>/; print "\n" if /^>/ and $. > 1' < File1 You might need to add the final newline.


1

awk variables are just used for their values, like variables in most languages - they aren't re-parsed for expressions. You can do what you want with this: a='15986327,415532694,850257614,875121642,20140819' echo $a | awk -F, -v OFS=, '{print $1, $2, $4}' (setting OFS so you get commas in the output). If having b as a variable is required, you can use: ...


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.


0

If you can use perl: $ perl -F',' -anle 'print if $. == 1 or (grep {$_} @F)[-1] =~ /^Failed/' file Name,Mon,Tue,Wed,Thu,Fri,Mon Linux,Successfull Fullbackup; Failed Fullbackup,,,,,Failed Fullbackup Unix,,,,Failed Fullbackup,, MAC,Failed Fullbackup,Failed Fullbackup,Failed Fullbackup,Failed Fullbackup,Failed Fullbackup,Failed Fullbackup


2

I wrote a book on sed—Definitive Guide to sed—that includes a tutorial. It fully covers sed, as well as related commands like grep, tr, head and tail. Also fully covers regular expressions much better than I've seen elsewhere. I agree with others that solid understanding regular expressions well is very important. I also agree that sed is best used for ...


2

Since this is a large file, it may be worth using a slightly more complex tool for a performance gain. Usually, specialized tools are faster than generalist tools. For example, solving the same problem with cut tends to be faster than grep which tends to be faster than sed which tends to be faster than awk (the flip side being that later tools can do things ...


1

With perl: perl -F'\t' -anle 'print if @F == 13' file to edit inplace, add -i option: perl -i.bak -F'\t' -anle 'print if @F == 13' file


2

You almost have it already: awk -F'\t' 'NF==13 {print}' infile > newfile And, if you're on one of those systems where you're charged by the keystroke ( :) ) you can shorten that to awk -F'\t' 'NF==13' infile > newfile


4

Try: $ awk '$4 >= 25 {print $1,$4}' file Fashion: 27.729519791085 Food: 101.729519791085


3

With awk, this is easy enough using an array, split, and a regular loop: { split($2, elements, ",") out = "" for (i in elements) { el = elements[i] if (!(el in used)) { out = out el "," } used[el] = 1 } sub(/,$/, "", out) $2 = out } 1 For each line, we split the second column by commas ...


4

perl -lpe 's/\s\K\S+/join ",", grep {!$seen{$_}++} split ",", $&/e' You can run the above like so: $ perl -lpe 's/\s\K\S+/join ",", grep {!$seen{$_}++} split ",", $&/e' afile A 1,2,3,4 B 5,6 C 15 How it works First calling perl with -lpe does the following 3 things. -l[octal] enable line ending processing, specifies line ...


3

If there are no other columns with commas, that resolves to counting the number of lines and commas: tr -cd ',\n' < file | wc -c


7

If there are no other columns with commas, this will do it: awk -F, '{c+=NF} END {print c+0}' file


4

Perhaps awk '{s += split($2, a, ",")}; END{print s+0}' file 12


2

Try: $ awk '{split($2,a,",");s += length(a)}END{print s+0}' file 12 or if your number is only one digit: $ awk '{gsub(",","",$2);s += length($2)}END{print s+0}' file 12


0

Please make sure to mention the file name twice as mentioned in the below code awk 'NR == FNR{if(FNR == line) {for(i=1; i<=NF; i++) {if($i > lmt) a[i]} close(FILENAME)} next} {for(i=1; i<=NF; i++) {for(i in a) {out = (out == "") ? $i : (out FS $i)}} print out; out=""}' line=3 lmt=5 file file


0

{ paste -d\| /dev/fd/3 /dev/fd/4 | sed '/\([^ ]*\) [0-9:/ ]*\(.*\)|\1 .*\2/d;=' | sed 'N;s/\(\n\)\(.*\)|/:\tFILEA: \2\1\tFILEB: /' } 3<<\FILEA 4<<\FILEB qaqa rara abc 10:12:25 08/20/2014 123456 def ghi fff ddd jkl 09:20:40 08/20/2014 978645 dfdf gggg FILEA qaqa rara abc 10:32:15 07/15/2014 121456 xxx ghi eee ddd jkl 10:01:22 07/15/2014 ...


9

How about this command? csplit logname.log /---\ LOG\ REPORT\ ---/ {*} Testing cat logname.log --- LOG REPORT --- Mary Had A Little Lamb --- LOG REPORT --- Her Fleece Was White As Snow After running the above command, the output I get is, cat xx01 --- LOG REPORT --- Mary Had A Little Lamb cat xx02 --- LOG REPORT --- Her Fleece Was White As Snow


2

Wrong tool, use either perl or awk e.g. cat > splitFileByLogReport.pl <<EOF #!/usr/bin/perl undef $/; $_ = <>; $n = 0; for $match (split(/(?=--- LOG REPORT ---)/)) { open(O, ">$ARGV[$argnum]" . ++$n); print O $match; close(O); } EOF Then to run: perl splitFileByLogReport.pl yourFile.txt


6

How about something like awk '/--- LOG REPORT ---/ {n++;next} {print > "test"n".out"}' logname.log


0

The easiest command to get result would be the below $ diff <(tr -s "[0-9],:,/" " " < fileA) <(tr -s "[0-9],:,/" " " < fileB) The command is very straight forward and there is no complex regular expression as well. Sample output will be as below 2,3c2,3 < abc xxx < ghi eee ddd --- > abc def > ghi fff ddd Hope this is what ...


2

If your timestamps are consistently formated, you could strip them off (with sed, for example) before processing the files with whatever differencing method, e.g. diff <(sed -E 's|[0-9]{2}:[0-9]{2}:[0-9]{2} [0-9]{2}/[0-9]{2}/[0-9]{2,4} [0-9]{1,} ||' fileA) <(sed -E 's|[0-9]{2}:[0-9]{2}:[0-9]{2} [0-9]{2}/[0-9]{2}/[0-9]{2,4} [0-9]{1,} ||' fileB) ...


1

Using awk: awk -F, '$9!~/Type 0/{print $0}' file AM_AUTO_1,1cx0,A166,B,C118,B,G129,B,A-minor Type I ACG AM_AUTO_2,1cx0,A165,B,C119,B,G128,B,A-minor Type II ACG AM_AUTO_3,1cx0,A166,B,C119,B,G128,B,A-minor Type II ACG AM_AUTO_4,1ddy,A25,A,C21,A,G8,A,A-minor Type I ACG Explanation: This causes awk to use , as delimiter (-F,). If the 9th field doesn't ...


1

With awk, you can do: $ awk '$(NF-1)!=0' file AM_AUTO_1,1cx0,A166,B,C118,B,G129,B,A-minor Type I ACG AM_AUTO_2,1cx0,A165,B,C119,B,G128,B,A-minor Type II ACG AM_AUTO_3,1cx0,A166,B,C119,B,G128,B,A-minor Type II ACG AM_AUTO_4,1ddy,A25,A,C21,A,G8,A,A-minor Type I ACG or using grep: grep -v 'Type 0' file


1

diff \ <(sed -r 's\[0-9]{2}:[0-9]{2}:[0-9]{2} [0-9]{2}/[0-9]{2}/[0-9]{4} [0-9]{6} \\' fileA) \ <(sed -r 's\[0-9]{2}:[0-9]{2}:[0-9]{2} [0-9]{2}/[0-9]{2}/[0-9]{4} [0-9]{6} \\' fileB) \ | egrep '^> ' | sed -r 's/^> //' > fileC Explanation Strip the irrelevant portions as given in the OP's question from fileA and fileB and feed this into diff. ...


1

I don't have nawk but this works for me (with gawk, using the --posix switch): awk 'BEGIN{FS=OFS=","}{sub(/0568/,"0808",$3)}1' file fedorqui's comment suggests that you may need to use -v OFS=',' rather than -vOFS=',' but if input and output separator are the same, the assignment in the BEGIN block isn't much longer. I've also changed your gsub to sub as ...


0

If the .csv is in DOS format another option might be: tr -d \\r <input.csv | sed 's/$/,new_col/' ...which would handle it all in one shot.


3

Your input csv file is in DOS format. Microsoft DOS/Windows use a Carriage Return (\r, ^M, ASCII code 13) followed by a Line Feed (\n, ^J or ASCII code 10) at the end of each line while Linux/Unix uses the Line feed only. To fix this, run: $ dos2unix file.csv dos2unix: converting file file.csv to Unix format ... $ awk '{print $0",new_col"}' file.csv ...


0

Would this work? awk -F, '{$(NF+1)="new_col";}1' OFS=, input.txt. This will output the new table in command line. In the example, I've added an output file (output.csv just in case). Before running the command: $ cat input.csv 68,110,0.362,0.101 11,98,0.405,0.101 57,94,0.374,0.100 61,123,0.342,0.100 124,130,0.298,0.100 Executing the command: $ awk -F, ...


1

This answer demonstrate awk and sed examples, both in command line output and in file changes. For awk, we'll use $ awk -F"," '{gsub("0568", "0808", $3)}1' input.txt and receive the changed string in command line output (or to variable), but the original file will not change. $ cat input.txt "20131008","0004","0568","98" $ awk -F"," '{gsub("0568", ...


-1

I tried this: nawk 'BEGIN { FS=OFS="," } { sub("0568","0808",$3) }1' abc.txt It works fine.


1

{ nl -bpH -w1 | sed 's/^\([0-9]*\)[ \t]*\([^H]*.\)/\2\1/' } <<\DATA ... 1562 first part 1563 H col3 H col4 1564 H col3 H col4 ... 3241 H col3 H col4 3242 third part DATA OUTPUT ... 1562 first part 1563 H1 col3 H col4 1564 H2 col3 H col4 ... 3241 H3 col3 H col4 3242 third part That's the fastest way I can imagine it ...


2

If you want the max length per file, with GNU awk: find . -type f -exec awk ' length>l {l=length} ENDFILE{print FILENAME ":", +l; l=0}' {} + Or the one max length in all the files: find . -type f -exec cat {} + | awk 'length>l {l=length}; END{print +l}' That assumes the files end in newline characters. If one file doesn't end in a newline ...


5

The most straightforward solution is to concatenate all the files and pipe the result to your script: cat ./* | awk '{ if ( length > L ) { L=length} }END{ print L}' You can also pass directly several files to awk: awk '{ if ( length > L ) { L=length} }END{ print L}' ./* Of course, there can be some warnings if files are in fact directories but it ...


1

With perl: perl -pe 's/\bH\b\K/++$i/e' file You can replace -pe with -pi.back -e for in-place editing with the original saved as file.back or -pi -e for no backup.


1

Try this: awk 'BEGIN { hNum = 1; } { if ($2 == "H") { $2 = "H" hNum; hNum++; } print $0; }' yourFile > outFile It run awk using space as separator, so $2 is the second token of each line and if $2 is equal to "H", then replace it with "H" followed by number started from 1. Finally print out line.



Top 50 recent answers are included