New answers tagged

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


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

This awk script will get you the difference in seconds between the timestamps in column two of the first two rows awk -F, '{ cmd = "date --date " $2 " +%s "; cmd | getline sec[NR] } NR>2 { exit } END { print (sec[2] > sec[1]) ? (sec[2] - sec[1]) : (sec[1] - sec[2]) }' <filename It can be broken down like this, too, if you prefer: awk -F, ' # ...


0

Here's how to get out the first two timestamps: head -2 mydata.csv | awk -F, '{print$2}' "Comparing" the times is a harder problem. If you just want to know which one is earlier, you can ask sort to check if they are in sorted order: if head -2 mydata.csv | awk -F, '{print$2}' | sort -c 2> /dev/null then echo "The first timestamp is earlier than ...


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


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


1

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


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.


1

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


0

if you need to remove always the same sentence you can try sed -e 's/abcd[0-9]*//g' -e 's/*test[0-9]*://g' file.ext this show you the output in the stdout if you want save this output simple add the > to the finel of the instruction or >> to append the content to another file sed -e 's/abcd[0-9]*//g' -e 's/*test[0-9]*://g' file.ext>new_file.ext sed -e ...


0

I would probably need to see some more examples, but I imagine something like this would work sed -e 's/\<abcd[0-9]\>//g' inputfile && sed -e 's/\<test[0-9]\>//g' inputfile


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]*:


0

I don't think there is any nice way to do it with just one replacement. But if four replacements are fine, proceed as follows: Replace \A' by  (that is, replace ' at the beginning of the string). Replace '\Z by  (that is, replace ' at the end of the string). Globally replace ([^[:alnum:]])' by \1 (that is, replace every sequence of a ...


2

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


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

awk '{ORS=""; print $0}' textfile <div id="crmpicco"> <div class="ayrshireminis">... content in here ... </div></div> Additionally, as was already pointed out, the tr utility is awesome for this. To remove newlines and tabs/spaces all at once using the tr utility, do: # cat textfile |tr -d '\n\r" "' ...


0

Go for don_crissti's (accepted) answer if you have GNU grep. Just in case you don't (e.g. on a standard Mac OS X, where that won't work), you could alternatively save this snippet to a bash script, e.g. myconvert.sh #!/bin/bash while IFS='' read -r line || [[ -n "$line" ]]; do if ! grep -Fq "$line" $2 then sed -i '' "/$(echo $line | sed -e ...


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


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


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


0

You could do something like this. I define the line as you have. Next, I parse our the first comma-separated token from the line (I assume here that will be valid for a variable name). Then I construct two new variables based on the first token, and assign them to the second and third comma-separated tokens from the line. line="dog,/path1,/path2" ...


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


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.


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


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


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


0

Ok, I think i've got a solution for the double index as well modifying the command of cas. This makes the job: perl -p -e 's/\[(\d+)\]\[(\d+)\]/"[" . ($1) . "][" . ($2+40) . "]"/ge' prova.txt Not sure by the way why I need the . ($1) . (point with blank space).


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


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


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


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


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


0

You can store file content into variable( it will store newlines) and then append to same file from variable. Just remember to use quotes around variable. x=$(cat test.txt) && echo "$x" >> test.txt Or using "tee" command you can append directly to same file, don't get confused when it first shows files original content in stdout, it has same ...


0

You might try catting the file, then using sed to do an append. So something like: cat in.txt ip 10.22.1.34 10.22.1.35 d=1 if [ ${d} -eq 1 ]; then cat in.txt | while read LINE; do sed -i '$a\'"${LINE}" in.txt; done fi cat in.txt ip 10.22.1.34 10.22.1.35 ip 10.22.1.34 10.22.1.35


0

A cat file.txt >> file.txt should do the trick, >> appends the content of stdout at the end of the specified file


1

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


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


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

with bash free and grep only read junk total used free shared buffers cached junk < <(free -m | grep ^Mem) echo $used


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


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


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

Here's another way with sed: sed -n '/<Car>/{x;/.\{2\}/{x;$!{n;p};q};s/.*/&./;x}' infile This is using the hold space to count. Each time it encounters a line matching <Car> it exchanges buffers and checks if there are exactly N-1 occurrences of a character in the hold buffer. If the check is successful it exchanges again, and if not on ...



Top 50 recent answers are included