Tag Info

New answers tagged

0

It seems that your files contain some special characters, like carriage return \r\n. You can check using: cat -et file If has, you should remove \r before using paste: tr -d '\r'


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


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


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


3

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


0

Give a try to this one: grep -Po 'LAN2:\K(\d{1,3}.){3}\d{1,3}/\d*' file This looks for a block of PATTERN:ipdigits.ipdigits.ipdigits.ipdigits/digits and prints the ipdigits.ipdigits.ipdigits.ipdigits/digits part (\K "cleans" the matching). It indicates that ipdigits must have between 1 and 3 characters. More filters could be applied (to match 1 to 255). ...


1

What about awk '{for(i=1;i<=NF;i++) if ( $i ~ /[^:]*:/ ) { n=split($i,A,":"); if (length(A[2]) && n==2) print A[2] ; }} ' b.txt X.X.X.X/XX X.X.X.X/XX X.X.X.X/XX X.X.X.X/XX Edit: (with LAN) awk '{for(i=1;i<=NF;i++) if ( $i ~ /[^:]*:/ ) { n=split($i,A,":"); if (length(A[2]) && n==2) printf "%s -- %s\n",A[1], A[2] ; }} ' b.txt LAN1 -- ...


1

To substitute the constants with expressions, you can use command substitution. To substitute the output of a command into an expression, use $(command) In this case, the appropriate command line is: tail -n+$(grep -nr "foo:" bigfile | cut -d':' -f1) bigfile | \ head -n$(($(grep -nr "goo:" bigfile | cut -d':' -f1)-$(grep -nr "foo:" bigfile | cut -d':' ...


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


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


0

Now I have a working solution BEGIN { SearchMinFlag = 0; TempLimit = 195.0 DeltaTime = 550 TempMin = "LEER" FS = "\t" } # Stop MinSearch NR > 2 && $1 > tstop {SearchMinFlag = 0} # MinSearch SearchMinFlag == 1 { ywert = $2; if (ywert < TempMin) { TempMin = ywert; TimeMin = $1 } } # Start ...


3

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


0

With GNU wc: cat *.txt|wc -L -L prints the length of the longest line.


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


-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 :-)


4

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



Top 50 recent answers are included