New answers tagged

2

As I understand it, you have a file text which is something like: $ cat text 1 2 3 4 5 And, you want to extract the 3rd and 5th lines and put them in the middle of some echo statements. In that case, try: $ { echo 'Some text'; echo 'Some text'; echo -n 'Some text'; echo -n "$(sed -n 3p text)$(sed -n 5p text)"; echo 'Some text'; echo 'Some text'; } ...


2

Simply: sed -e '1h;G;s/\n/ /' <file


2

Reading the manual ? However first sed replaces space and semicolon with colon awk uses colon as separator and print the first, second, until the 34th fields, separating with space the last sed changes the space in colon


1

sed -i -e 's/<\/link>/ /g' file.txt the / is a command separator for sed, so you need to escape it with \


3

In awk: { data[$2, $1] = $3; } END { split("OTU0 OTU1 OTU2 OTU3", rows); split("OTU_ID PC.354 PC.355 PC.356", cols); for (i = 1; i <= 4; i++) { printf("%10s", cols[i]); } print ""; for (i = 1; i <= 4; i++) { printf("%-10s", rows[i]); for (j = 2; j <= 4; j++) { item = data[rows[i], ...


4

In perl: #!/usr/bin/perl my (%hotu, %hpc)=(); while(<>){ my($pc,$otu,$v)=split; $hpc{$pc}=1; ($hotu{$otu} or $hotu{$otu}={})->{$pc}+=$v; } #headers my @apc = sort keys %hpc; print join ("\t", 'OTU_ID', @apc) . "\n"; #values foreach my $otu (sort keys %hotu) { print join ("\t", $otu, map {$_=0 unless defined; $_} @{$hotu{$otu}}{@apc}) . ...


0

awk -F, -v OFS=, '/^[[:blank:]]+/ { str=gensub(/ /,",","g",$0); sub(/^,+[^,]+,/,"",str); next }; !/^[[:blank:]]+/ {print str,$0}' nick.txt (this can, of course, be all on one line. I wrote and tested it as a one-liner, and then added line-feeds and ...


5

dig can read in a file containing a list of hostnames and process them one by one. You can also tell dig to suppress all output except the answer section. This should give you the output you want: dig -f hostlist.txt +noall +answer +search | awk '{sub(/\.$/,"",$1); print $1","$5}' awk's sub() function is used to strip the literal period . from the ...


1

sed -i -e '/Leonardo/ s/$/ Narnie/' database_readers.txt This uses sed's in-place edit option (-i) to search for all lines matching the regexp Leonardo, and replace the end-of-line anchor ($) with a single space and the word Narnie. This effectively appends the Narnie to the end of the matching lines. Be careful with the regexp search pattern - it's easy ...


0

Consider this sample file: $ cat sample.html <li a=x>Point One </li> <li>Point Two </li> I believe that this sed command does what you ask (this may require GNU sed): $ sed -Ez 's|<li\b|\t<li|g; s|\n</li\b|</li|g' sample.html <li a=x>Point One</li> <li>Point Two</li> How it ...


4

Change your invocation of gawk to the following: | gawk '{print substr($1,1,length($1)-1)","$NF}' >fqdn-ip.csv


16

The sed command, the awk command, and the removal of the trailing period can all be combined into a single awk command: while read -r host; do dig +search "$host" ALL; done <hostlist.txt | awk 'f{sub(/.$/,"",$1); print $1", "$NF; f=0} /ANSWER SECTION/{f=1}' Or, as spread out over multiple lines: while read -r host do dig +search "$host" ALL done ...


3

Here's one way to do it: sed '/,/!{ # if there's no comma on this line y/ /,/ # translate spaces to commas h # copy pattern space over the hold buffer d # delete pattern space } //{ # if the line contains commas G ...


3

nl is ideally suited: nl -v2 -p -ba will start counting from 2 (-v2), ignoring page changes (-p) and numbering all lines (-ba).


0

With pure bash script? i=2; cat output.txt | while IFS= read -r line; do echo "$i $line" i=$((i+1)) done


1

POSIXLY: awk '{printf("%s %s\n", FNR+1, $0)}' file If you want to pass parameter: awk -vn=2 '{printf "%s %s\n", n++, $0}' <file If you want only the range is produced in case the file is longer than the range: awk -v s=2 -v e=4 'BEGIN{for(n=s;n<=e;n++)print n}' | paste -d' ' - file


0

The reason your sed command doesn't work is that it assumes you have a date on every line, which is not the case if some lines come from multi-line error messages. When there is nothing matching the replacement pattern, sed does no replacement and the call stack listings you saw stay in the output. To get only the dates from lines that have them in the ...


2

Perl solution: perl -ane 'BEGIN { $" = "\t" } $F[0] =~ /(..)$/, $F[-1] = { "01" => 2, "99" => 1 }->{$1} || $F[-1]; print "@F\n" ' input_file > output_file -n reads the input line by line. -a splits each line on whitespace into the @F array. $" is set to tab so that the array members are tab separated in ...


2

You could do this somewhat cryptically using nested conditional operators ?: (aka ternary operators) awk '{$2 = $1 ~ /-01$/? 2: $1 ~ /-99$/? 1: $1; print}' input PE01-02-01 2 PE01-02-99 1 PE01-03-01 2 PE01-03-99 1 PE01-05-01 2 PE01-05-99 1 Alternately awk '{n = split($1, a, "-"); $2 = a[n] == "01" ? 2: a[n] == "99"? 1: a[n]; print}' input


0

When I'm confronted with needle-in-haystack work like this, I like to turn it into a line-oriented problem if I can. You might be able to do that with something like this: $ sed -E 's:[0-9]+:\n&\n:g' filename \ | grep -F -A1 '/pbs.twimg.com\/profile_images\' | tail -1 That puts any digit-string a line by itself, greps for your string, and prints the ...


0

awk '/^[0-9]{2}[.][0-9]{2}[.][0-9]{4}/ {DATES[$1]++} END{ for(d in DATES) {print d} }' Unlike rock, paper, scissors: awk always beats sed. :-) Edit: here is is in action: $ cut -b-60 t 15.04.2016 13:13:30,228 INFO [wComService] [mukumukuko@sy 15.05.2016 13:14:10,886 INFO [wComService] Call 5303 from 15.06.2016 13:14:20,967 INFO ...


1

sed '/foo$/{n;s/^#bar/bar/;}' is a literal translation of your requirement. n is for next. Now that doesn't work in cases like: line1 foo #bar line2 foo #bar Or: line1 foo line2 foo #bar Which you could address by doing: sed ' :1 /foo$/ { n s/^#bar/bar/ b1 }'


0

use this: awk '{print $1}' fac.log | sort | uniq


2

Use the N;P;D cycle and attempt to substitute each time: sed '$!N;s/\(foo\n\)#\(bar\)/\1\2/;P;D' infile this removes the leading # from #bar only if it follows a line ending in foo otherwise it just prints the pattern space unmodified. Apparently, you want to uncomment US mirrors in /etc/pacman.d/mirrorlist which is a whole different thing (please edit ...


1

Try: sed -e '$!N;/foo\n#bar/s/\(\n\)#/\1/;P;D'


0

You almost have it. Step1 On GNU and Linux and perhaps other systems, the date command can be used to print out an arbitrary format for a time specification given a user-friendly time expression. For instance, I can get a string representing the time from 15 minutes in the past using this: date --date='15 minutes ago' You've pretty much done this with ...


0

I'd use: awk -v limit="$(date -d '15 minutes ago' +'[%FT%T')" ' $0 >= limit' < log-file That ignores the potential problems you may have two hours per year when the GMT offsets goes from -04:00 to -05:00 and back if daylight saving applies in your timezone. date -d is GNU specific, but you're using it already.


0

Regular expressions aren't a good choice for matching timestamps, as they don't really 'understand' numeric values. So instead, I'd suggest parsing the timestamp: #!/usr/bin/env perl use strict; use warnings; use Time::Piece; my $now = time(); my $last = $now - 15 * 60; while ( <> ) { my ( $timecode ) = m/\[([^\.]+)/; print $timecode; ...


-1

Please give a try with xml_grep 'id' file.xml --text_only


0

I'd pick up perl, because then you can split and join: #!/usr/bin/env perl use strict; use warnings; while ( <DATA> ) { chomp; my @fields = split /;/; s/\s+//g for @fields; print join ";", @fields, "\n"; } __DATA__ 03139; 5; IT1234978208; 20150930 ; CTZ 13/31.12.15; 03137; 6; IT1234978206; 20151015 ; CTZ 13/31.11.18; ...


3

Limit the * to non-spaces: sed -i 's/-agentpath[^ ]*//' files


6

It looks like you want to remove only those spaces that are before or after a semicolon. In that case: $ sed 's/[[:space:]]*;[[:space:]]*/;/g' text 03139;5;IT1234978208;20150930;CTZ 13/31.12.15; 03137;6;IT1234978206;20151015;CTZ 13/31.11.18; .... .... 03134;8;IT1234974406;20151212;CTZ 13/37.13.17; How it works: The sed substitution command typically ...


0

Your output contains any amount of random cruft, and one line you are interested in. Select that line, discard anything but the ID and print the result: sed -n '/profile_images/s/.*profile_images\\\/\([0-9]\+\).*/\1/p' This could be made slightly more efficient by quitting immediately after that line has been processed. In fact, that is pretty much ...


2

Try this: awk '/^[*][*][*] /{ if ($0 in seen) fname=$0; else seen[$0];} fname{print>fname}' file How it works awk implicitly reads a file line by line. For each line read, we do the following: /^[*][*][*] /{ if ($0 in seen) fname=$0; else seen[$0];} For any line that begins with three stars and a space, we check to see if we have seen that line ...


0

I could have it confirmed (thanks to the #toybox irc channel) that the issue was indeed a bug in toybox sed, which was supposed to have been solved, but Android M still ships with an outdated version of toybox. These are the links to the toybox mailing list (which alas, I had missed) where the issues had been discussed: ...


3

First, save the base64-encoded data in a file called, e.g., base64.txt. For example: base64 < originalfile > base64.txt Then: printf '%s\n' '/BASE64/r base64.txt' 1 '/BASE64/d' w | ed FILENAME This uses ed to search in FILENAME for a line containing the string BASE64, insert the contents of base64.txt after that line, go back to the first line, ...


3

Another option would be to replace sed with ed and store your commands in a file. For example, if you create ed_cmds with the following contents: %s/BASE_64/<expanded variable>/g w q you could then run < ed_cmds ed FILE_NAME and it would make the changes you wanted, so instead of setting $BASE_64 you'd create the ed command file. Ed ...


2

The first regex searches for any line containing the following: '^ - start of line, followed by [^,]* - 0 or more non-comma characters, followed by , - a comma, followed by [^0] - any single character other than a zero, followed by [^,]* - 0 or more non-comma characters, followed by ,' - a comma grep -c counts the number ...


2

The size of the Base64 representation of the file ($BASE_64) is too long and exceeds maximum argument size. You should be able to see this limit for your system by running getconf ARG_MAX You have to increase the size of the ARG_MAX value. But I think if the file is too big, then you will have to use a different approach to do this replacement. If a ...


1

grep -c '^[^,]*,[-+0-9.]*[1-9]' That should cover for numbers expressed as 12, -1, 0e+12, 01, 0.0001. But not for 0xFF or Inf or NaN for instance, so that would still be different from the more canonical: POSIXLY_CORRECT=1 awk -v n=0 -F , '$2 != 0 {n++}; END{print n}' If your input has numbers expressed in such a format. For a sed only solution, you ...


1

With grep: grep -c '^[^,]*,[^0]' <file That's only work if 2nd column is formed like integer, but not -0, +0. For more general case, see @St├ęphane Chazelas's answer.


1

You can use the -c option of grep. And you can remove all chars up to the first comma and everything from the second comma on with sed: sed 's/^[^,]*,//;s/,.*//' < the_file | grep -c -E '[^0]' EDIT: This sed command does the same as your cut command so you should also be able to use your original grep command. EDIT2: If you want to use only one ...


0

What about echo test/{1999,2000,2001}/test or even echo test/{1999..2001}/test


1

Here is one bash solution as well : declare -a a=(1999 2000 2001) url='/test/test' for i in "${a[@]}" ; do echo "$url" | sed "s:/:/$i/:2"; done


1

You specify only one "URL" the following Python program support a list of them: urls = [ '/test/test', ] years = [1999, 2000, 2001] for url in urls: for year in years: spliturl = url.split('/') spliturl.insert(2, str(year)) print('/'.join(spliturl)) The trick is to insert the year at the second position, the first ...


1

A perl solution: $ perl -lpe '$_ .= ".mtt.corp" if !/\.mtt\.corp$/' file linuxA.mtt.corp linux3V.mtt.corp linux4B.mtt.corp linux2A.mtt.corp linux5v.mtt.corp The -p makes perl print each line of the input file after applying the script given by -e and the -l removes trailing newlines from each input line and adds one to each print call. The $_ variable is ...


4

It's quite simple with sed: sed -e '/\.mtt\.corp$/!s/$/.mtt.corp/' <file That does substitute end of line with .mtt.corp on each line which does not end with that string.


0

have a try pup (for OSX: brew install https://raw.githubusercontent.com/EricChiang/pup/master/pup.rb) u can use CSS3 selector and extractor to get value from href attr


6

If you get some command that works but you don't understand the options use man command to get more information about the options used. man sed will show you (if you scroll/search through the output): -n, --quiet, --silent suppress automatic printing of pattern space and = Print the current line number. and (searching for $ can be ...


1

= prints the current line number. $ is the last line in the file. -n suppresses the usual output.



Top 50 recent answers are included