Hot answers tagged

7

Use perl: perl -pe 's/(?<=\[)(\d+)(?=\])/$1+1/ge' prova.txt Explanation: -p means loop over every line and print the result after every line -e defines the expression to execute on every line s/from/to/ does a simple substition s/(\d+)/$1+1/ge matches one or more digits, captures it into $1, and then the e modifier on the end tells perl that the ...


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


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.


4

You could also use awk: awk -F '[\\[\\]]' '{if ($2) { sub($2, $2 + 3)}} 1' prova.txt In fact, this can be slightly shortened to: awk -F '[\\[\\]]' '$2 { sub($2, $2 + 3)} 1' prova.txt


4

(\w+ ) will match a word or part of word. This means that grep will treat every character in every word as a potential start-of-match. In your example, it will consider each of word1 ord1 rd1 d1 1 before moving on to the successful match (starting at word2). As you are interested in finding whole words, you can prevent all the attempted mid-word ...


3

You could split the 2nd field on : and if you get more than 2 pieces (that is, the number of elements in array z) keep only the 1st one: awk '{n=split($2, z, ":");if (n > 2) $2=z[1]};1' infile If you wanted to use sub you could do something like: awk '{sub(/:.*:.*/,"",$2)};1' infile that is, attempt to replace two colons (or more).


3

perl -p -e 's/\[(\d+)\]/"[" . ($1+3) . "]"/ge' dad85.txt This perl one-liner replaces any positive integer number (\d+) inside square brackets with that number incremented by 3. It uses the e regexp modifer to make perl evaluate the replace portion of the s/// operation as an expression. Output: (freqBiasL2[30]) (SatBiasL1[30]) (defSatBiasL2_L1[30]) ...


2

So there are a few ways. To summarize the comments above you can do one of the following: awk: awk '!/^WARN/' filename sed: sed '/^WARN/d' filename grep: grep -v '^WARN' filename


2

Here's an awk solution: awk 'NR==FNR{z[$0"-ADM"]++;next} {p=$3;sub(/.*-P/, "", p); if ($2 in z && p > 2) {print $1,$3 > "stoplist.lst"; $0="#"$0}}1' sites.lst master.tbl This reads sites.lst first and sets $0"-ADM" (that is the site name + the string -ADM) as an index of the array z. It then processes master.tbl extracting the value after -P ...


2

There may be a more shell-centric solution (awk?) but I usually turn to perl when the problem gets this complex. Here's a perl script that reads all of csv2 into memory, collecting the lines as keys into a hash whose values are the corresponding titles. It then loops through csv1, pulls out the title, then for each title from csv2, counts the number of ...


2

I know you've already solved your problem, but FYI you could have solved it with a very simple modification to your code : by inverting the sequence you loop over. Using {107..27..-1} (or more concisely {107..27}) would have been enough to solve your problem, since when replacing 30, only the original 30 would have been found, the 27 having not been ...


2

Here's a bash-centric solution: IFS=, read -r -a vars <<<"$line" printf "%s\n" "${vars[0]}=${vars[1]},${vars[2]}" "${vars[0]}1=${vars[1]}" "${vars[0]}2=${vars[2]}" The first line splits your $line variable into pieces based on the commas. The second line, working from the inside-out: outputs the first value (e.g. 'dog'), equals-sign, then the ...


2

You can format the datafile with any of several scripting languages. With awk, awk '/^[0-9]+$/ { printf "%04d\n", $0; next; } { print; }' < oldfile >newfile Similarly, you can format numbers with leading zeros using printf in bash, zsh, etc.


2

I think that's the thing you're trying to do: line='dog,/path1,/path2' IFS=', ' read -r -a dog <<< "$line" echo "$dog" dog echo "${dog[1]}" /path1 echo "${dog[2]}" /path2 This assumes that your shell is bash.


1

If you have gnu grep you could run: grep -oFf file1 file2 | sort | uniq | grep -Ff - file1 remove the last grep if don't need to preserve the order of the lines in file1. If you don't have access to gnu grep, with awk: awk 'NR==FNR{z[$0]++;next};{for (l in z){if (index($0, l)) y[l]++}} END{for (i in y) print i}' file1 file2


1

Using awk, being careful to pass the second file as the first parameter: awk '(NR==FNR) { a[NR]=$0 } (NR != FNR) { for (i in a) { print $0 a[i] } }' file2 file1 This saves all of file2 into an array a, then loops through file1 (implicitly) and loops through every element of a, printing the current line of file1 followed by the saved line from file2. The ...


1

for w1 in $(cat file1) do for w2 in $(cat file2) do word=${w1}${w2} echo ${word} >> permutationfile done done for different formatting options, you can play with printf instead of echo command


1

After the line my @titlewords = split /\s+/, $title; #/ get words add the code to remove the words from the array: my @new; foreach my $t (@titlewords){ push(@new, $t) if $t !~ /^(and|if|the)$/i; } @titlewords = @new;


1

This shell script will get you the difference in seconds between the timestamps in column two of the first two rows: ( IFS=, read -r _ a _; IFS=, read -r _ b _; a=$(date --date $a +%s); b=$(date --date $b +%s); echo "$a - $b" | bc | tr -d - ) <filename It can be broken down like this, too, if you prefer: ( IFS=, read -r junk a junk # Get ...


1

A solution using cut: cat $FILE | cut -d' ' -f4,6 --complement


1

You can try sed substitution $ sed -e 's/abcd[0-9]*//g' -e 's/*test[0-9]*://g' file Where abcd[0-9]* matches every abcd followed by a number, same with *test[0-9]*:


1

sort -t_ -k2 infile will sort data using the 2nd key with underscore as separator.


1

This is closely related to another question only in this case the substitution has to be made on the first line that matches clockBias after the pattern. If there was no special character in your fileB you could run with gnu sed : sed -E 's|(.*)[[:blank:]](.*)|/\1/,/clockBias/{/clockBias/{s/(:[[:blank:]]{1,})(.*)/\\1\2/}}|' fileB | sed -Ef - fileA That ...


1

If you are using bash, you can do something like this (with extra echo statements to show what's happening at each stage): $ line=dog,/path1,/path2 $ newline=$(printf "%s\n" "$line" | perl -n -e 'BEGIN {$count=1}; my ($var,@values) = split /,/; print "$var=" . join(",",@values); ...


1

I like joseph's answer but needed it to strip // comments also so I modified it slightly & tested on redhat # no comments alias alias nocom="sed -E '/^[[:blank:]]*(\/\/|#)/d;s/#.*//' | strings" # example cat SomeFile | nocom | less I bet there's a better way to remove blank lines than using strings but it was the quick & dirty solution I used. ...


1

@MelBurslan's comment is correct, you shouldn't in general expect people to write scripts for you, as that's not what StackExchange is for. But in this case, I had some time: here's one script that does what the question asks, assuming that the XML doesn't contain any newlines, and that the subdirectory and filename portions don't contain any special ...


1

You can do something like: cat in.txt > /tmp/tmp.txt && cat /tmp/tmp.txt >> in.txt


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



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