Hot answers tagged

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


3

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


3

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


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


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

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


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


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


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


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 }'


1

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


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


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



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