New answers tagged

0

#! /bin/ksh inputfile='file1.txt' sqlfile='select.sql' S_ids=$(awk -F"|" '{gsub(/^|$/,"\\'\''",$1) ; print $1","}' "$inputfile" | xargs | sed -e 's/,$//') sed -i "s/FLAG/${S_ids}/g" "$sqlfile" Output: $ cat select.sql SELECT * FROM CUSTOMERS WHERE ID IN ('1234', '1345', '8427', '2132', '3243'); This uses awk's gsub() function ...


2

The reason this doesn't work can be inferred from the error message (that you omitted to provide): sed: -e expression #1, char 14: unterminated `s' command The sed command does not accept a multi-line value. You have to collapse your multiple lines into a single line. You could do this with a script such as this: #!/bin/ksh S_ids="'$(cut -d'|' -f1 ...


0

There are 2 ways I recommend going about this. 1) Put a function in your bashrc / bash_profile and create an alias to call that function (this will make global usage of this) 2) create a shell script file and can create an alias of that file as well. #!/bin/bash function matchString(){ REGEX="$1" FILE="$2" RESULTS=$(grep -n "$REGEX" $FILE | awk -F ":" ...


0

Try this awk. echo "ABC123xxx:: 2345 ABC345yyy:: 5678 ABC986zzz:: 7955" | awk '{for(i=1;i<=NF;i++) if(match($i,"ABC")>0) line=line "\n"$i;else line=line $i; sub("^\n","",line); print line}' Creates a line with all fields, adding in front of the fields that begin with "ABC" a line break. Finally it eliminates the first line break and prints


0

sed -E -e 's/ (ABC)/\n\1/g' The sed command replaces any instance of ABC with a newline followed by ABC. It uses the () to capture part of the match (ABC without the leading space) and \1 to include it in the replacement. e.g. $ echo 'ABC123xxx:: 2345 ABC345yyy:: 5678 ABC986zzz:: 7955'| sed -E -e 's/ (ABC)/\n\1/g' ABC123xxx:: 2345 ABC345yyy:: 5678 ...


0

With sed: $ sed -e 's/ ABC/\ ABC/g' <file ABC123xxx:: 2345 ABC345yyy:: 5678 ABC986zzz:: 7955


0

This is a small improvement to Gilles awk solution (thanks Gilles!), but does require nawk: nawk '{if (/\\$/) {$0=substr($0,1,length($0)-2); printf "%s",$0} else print}' This will create a continuous line if the line wraps, but does not include the "\" and space character. (I found this helpful when grepping for PATH statements since the "\" can lead to ...


2

Another perl, without slurping whole file in memory: perl -pe 's/oooo/++$i/ge; $i = 0 if /endsnippet/' <file


0

I use the following awk code, it seems work very well. awk 'BEGIN {x=0} { if ($0~/oooo/) {x++; gsub(/oooo/,x); print $0} else if ($0~/snippet/) {x=0; print $0} else {print $0}}' demo.snippet edit: this can be rewrite as awk 'BEGIN {x=0} /oooo/ {x++; gsub(/oooo/,x); print $0 ; next } /snippet/ {x=0; } {print } ' ...


2

A possible perl solution perl -00 -F/\nendsnippet\n/ -pe '$n=1; s/oooo/$n++/ge' demo.snippet


2

With python (version 2 or 3): from __future__ import print_function oooo = None with open('demo.snippet') as fp: for line in fp: if line.startswith('snippet '): oooo = 1 while 'oooo' in line: line = line.replace('oooo', str(oooo), 1) oooo += 1 print(line, end='') This also works if there ...


2

Since it's either all spaces or all tabs you could pipe it to sed 'H;$!d;g;: m;/\n[^\n[:blank:]]/!s/\n[^\n]/\n/g;t m;s/.//' That's gnu sed (I don't think other seds support [\n]). It works by appending each line to the Hold buffer and then deleting it if it's not the last one ($!). On the last line it copies the hold space content over the pattern space ...


1

As stated by steeldriver, if you use GNU sed, you can tell which occurrence should be replaced, e.g.: echo "/test/test/ 12 /test/test" | sed -n -e 's_/._/_2 p' If you can not make use of this feature you can also write: echo "/test/test/ 12 /test/test" | sed -n -r -e 's_(/[a-z]([a-z]+))\1_\1/\2_ p' Biliography: http://www.grymoire.com/Unix/Sed.html ...


0

if you are only concerned with the first line of the output then limit what sed looks at by only addressing line 1: printf ' some text\n\n some more text\n' |sed '1s/^[ \t]*\([^ \t]\+.*\)$/\1/' this will ignore any white space at the beginning then match from anything that isn't white space plus the rest of the line and only on line 1.


0

I tried the following on a jessie pi and had no problem with stdbuf -oL in front of hexdump working as intended. while sleep .5;do echo -n x;done | stdbuf -oL hexdump -v -e '1/1 "%02x\n"' | ts %.s | cat The while is to simulate slow input, ts timestamps each line, and the cat is just to provide another pipe. The output is regular and the timestamps differ ...


0

echo "/test/test/ 12 /test/test" | sed 's/\/test\/t/\/test\//'


1

sed 's/abcd\(X[0-9][a-z]ad\)45das/\1/g' your_file_name should do it.


5

sed does not understand \d. You can use [0-9] or, more generally, [[:digit:]] in its place: $ sed -r 's/.*(X[[:digit:]])(.*)45.*/\1\2/' test.txt X1yad X2fad X3had X4wad X5mad


0

You sed does not understand the special sequence \d. Replace \d with [0-9] or character class [:digit:]: $ cat file.txt abcdX1yad45das abcdX2fad45das abcdX3had45das abcdX4wad45das abcdX5mad45das $ sed -nr 's/.*(X\d)(.*)45.*/\1\2/p' file.txt $ sed -nr 's/.*(X[0-9])(.*)45.*/\1\2/p' file.txt X1yad X2fad X3had X4wad X5mad


1

If the only thing in your file is a WHERE clause as described in your post, then this should do: $ sed -r "s/(\(|,)([^ ),]+)(\)|,)/\1'\2'\3/g" <Filename> where c1 in ('a') and c2 in ('a',b,'c') and c3 in () If your file is full of other data, like the rest of that SQL statement, or multiple SQL statements, then this may interfere with other text in ...


3

You can also do it with a loop: sed -e ':top' -e 's/\([(,]\)\([^),'\'']\{1,\}\)\([),]\)/\1'"'\2'"'\3/;t top' Or, using -E: sed -E -e ':top' -e "s/([(,])([^),']+)([),])/\1'\2'\3/;t top"


3

sed -r "s/,/','/g; s/\(([^)]+)\)/('\1')/g" Let's see how this changes the input you provided: where c1 in (a) and c2 in (a,b,c) and c3 in () First, commas are wrapped in quotes (s/,/','/g): where c1 in (a) and c2 in (a','b','c) and c3 in () Then we add quotes inside non-empty parens (s/\(([^)]+)\)/('\1')/g): where c1 in ('a') and c2 in ('a','b','c') ...


1

First of all: The last double quote you used is ” change it to ". Then: The main issue is that you are using / for the s option of sed but your string also contains (un-quoted) /. The simplest solution is to change to # for the s delimiter, as this: sed -i -e "s#test.smokeTest('(.*?)', '(.*?)', '(.*?)')#test.smokeTest(‘username’, ‘password’, ...


0

The same time comes from buffering. Try to find the command which buffers in the in the queue and try the command unbuffer.


4

A stream editor is a special type of filter. A filter is a program that takes text on standard input, does some magic, and spits it out on standard output. grep, and basically all of the coreutils are filters. A stream editor is a special type of filter: It applies one or more editing commands on the incoming text. In awk, the following three functions ...


2

try using other chars rather than / for separation maybe? sudo sed -i "s@listen = 127.0.0.1:9000@listen = '/var/run/php56-fpm.sock'@g" /etc/php-fpm.d/www.conf OR sudo sed -i "s/listen = 127.0.0.1:9000/listen = '\/var\/run\/php56-fpm.sock'/g" /etc/php-fpm.d/www.conf The problem is that you are not escaping the / as \/ but using @ as separator will fix ...


3

First of all, A single quote may not occur between single quotes, even when preceded by a backslash., ref Bash Manual Second, you may want to use some other char as separator instead of / as you have / in the replacement string. So as a result: sudo sed "s#listen = 127.0.0.1:9000#listen = '/var/run/php56-fpm.sock'#g" /etc/php-fpm.d/www.conf don't ...


2

grep can do that: grep -v '^..00' inputfile


2

There are lots of ways, e.g., awk '/^..00/{ next; }{ print; }' < mydata sed '/^..00/d' < mydata


3

Using "awk" This will print lines with N0 < LIMIT: # -v sets variables which can be used inside the awk script awk -v LIMIT=10 ' # We initialize two variables which hold the two previous lines # For readability purposes; not strictly necessary in this example BEGIN { line2 = line1 = "" } # Execute the following block if ...


-1

Running tr -d '\r' on the file before removing the lines work. No idea why, but when I ran this command it revealed another tag as well as a space before <\/File2>.


1

For the record, you could also do this with sed: sed -s '$!N /.*PATTERN.*\n/{/\n.*PATTERN/{x;/^1$/!s/.*/1/;b v};//!{x;/^1$/{s/./0/;b v};//!D}} //!{${/PATTERN/{x;/^1$/{b v}}};D;};: v;x;P;D' file1 file2 ... fileN That's gnu sed. With other seds you'd have to process one file at a time: sed '$!N # if not on the last line pull in the next ...


2

I'll use the same test file as thrig: $ cat file a pat 1 pat 2 b pat 3 Here is an awk solution: $ awk '/pat/ && last {print last; print} {last=""} /pat/{last=$0}' file pat 1 pat 2 How it works awk implicitly loops over every line in the file. This program uses one variable, last, which contains the last line if it matched regex pat. ...


2

One way would be to save the previous line, and print when the current and previous line both match: bash-4.1$ (echo a; echo pat 1; echo pat 2; echo b; echo pat 3) a pat 1 pat 2 b pat 3 bash-4.1$ (echo a; echo pat 1; echo pat 2; echo b; echo pat 3) | \ perl -nle 'print "$prev\n$_" if /pat/ and $prev =~ /pat/; $prev=$_' pat 1 pat 2 This will ...


-2

Hi there are various command which can help you fine last line try this.. <grep command> | tail -1 or awk '/result/ { save=$0 }END{ print save }' filename


0

If you only want the text in between ] and apal, perl is a good choice perl -lne '/(?<=\])(.+?)(?=apal)/ and print $1' file If you have GNU grep installed (via homebrew for example), you can grep -Po '(?<=\]).+?(?=apal)' file


0

Using "awk": #!/bin/sh awk ' function print_section() { # Only print section if "1234" was encountered if (valid == 1) print section; } { if (/something/) { # Start new section section = $0; } else if (/^\s*$/) { # Empty line -> output previous section ...


0

Use sed command : sed -i 's/<original text>/<new-text>/g' file.txt sed -i 's/original/new/g' file.txt Explanation: sed = Stream EDitor -i = in-place (i.e. save back to the original file) The command string: s = the substitute command original text = a regular expression describing the word to replace (or just the word itself) new text = ...


2

With awk: $ awk '$(NF+1) = 2.5' file If you want awk to do all the work for you: $ awk ' FNR == NR { s += $5; i++; next } { $(NF+1) = s/i; print } ' file file 2 2 2 2 2 2.5 3 3 3 3 3 2.5 4 4 4 4 4 2.5 1 1 1 1 1 2.5


0

Definitely we can use awk, try this one awk -v RS="END" '$0~/Frankfurt/{print $0 RS}' file


1

Not sure awk is the correct tool for that job since your search is over multiple lines based. I would think it's a job for perl. Going through your file you can use (similar to your awk statement): if (/^START/ .. /^END/){} in there you store your lines in an array, that you're going to print if say Frankfurt is met (use a boolean here): push @lines, $_; ...


2

With pcregrep: pcregrep -M '(?s)\{[^}]*PATTERN.*?\}' Or with GNU grep provided the input doesn't contain NUL bytes: grep -Poz '.*(?s)\{[^}]*PATTERN.*?\}'


4

You might need a word stemming algorithm for this. For example, Lingua::Stem is a word stemmer module written in Perl. If this fits your needs, you would need to install Lingua::Stem via CPAN. Then, the following Perl script would do the job: #!/usr/bin/perl require Lingua::Stem; # Read lines into array chomp(my @words = <STDIN>); # Stem in ...


2

Your attempts aren't working because you're trying to use shell operators such as && and > in the command executed by find, but you're typing those operators directly in the command, so they're executed by the shell that's calling find. Your commands are parsed as find … > tmp.$$ && mv … e.g. the first find invocation is find ./ ...


1

sed 's|^export PATH$|JAVA_HOME=/usr/lib/jvm/java-1.8.0-ibm-1.8.0.2.10-1jpp.1.el7.x86_64\ & JAVA_HOME|' < "$yourfile"


1

If you would have taken the time and read the grep manual, you would have found the l option -l, --files-with-matches Suppress normal output; instead print the name of each input file from which output would normally have been printed. The scanning will stop on the first match. Your find command would look like find . -name "*.tex" -exec ...


3

echo -n "$filename|"; tr "\n" "," <"$filename"


1

Do you really mean to merge the lists of values? That is port_defined=1,3,5 port_defined=2,4,123 should be combined into port_defined=1,2,3,4,5,123 If so, you could try BEGIN { FS = "=" } NR == FNR && /ports_defined/ { ports=$2 } NR > FNR { if ($1 == "ports_defined") { ports = ports "," $2 split(ports, p, ",") ...


0

Fun puzzle! In Perl: #! /usr/bin/env perl use strict; use warnings; my $prev; while (<>) { $prev = $_, next unless defined $prev; # prime the pump if ($prev ne $_) { print $prev; $prev = $_; # first half of a new pair } else { undef $prev; # discard and unprime the pump } } ...


3

The way you've written the script, the changes are made to the main C++ source file; thus the first time it runs, you replace 30.0 with 30, and the second time it runs, the first sed won't find anything to replace... The following should work (also fixing the quoting issue): #!/bin/bash Rpl_array="30 60 90 120 150 180 210 240 270 300 330 360 390 420 450 ...



Top 50 recent answers are included