Tag Info

New answers tagged

1

I found a solution using perl substring function echo leng-1323-006|perl -lpe 'substr($_, 5, 5) = "";' return correctly leng-006 I hve removed from position 5 the 5 bad characters include the -


1

POSIXly: $ sed -e 's/-[^-]*-/-/' file leng-001 leng-002 leng-004 leng-005 leng-006


2

Using sed: sed -r 's/(.*[a-zA-Z]{4}\-)([0-9]{4})/\1aaaa/g'


1

If there is no other limitation here your script sed '/^HEADER/{:1;N;/TAIL/!b1;/DOG/d}' text Just for fun other variants of awk: one: awk ' BEGIN{prn=1} /^HEADER/{block=1} block{ if(/DOG/) prn=0 if(!/^TAIL/){ line=line $0 "\n" next } } prn{print line $0} /^TAIL/{ ...


2

With perl: perl -0777 -lne 'print for grep !/DOG/, /^HEADER.*?TAIL.*?\n/mgs' your-file With awk: awk '! inside {if (/^HEADER/) {inside = 1; skip = 0; content = ""} else next} /DOG/{skip = 1; next} ! skip {content=content $0 "\n"} /^TAIL/ {if (!skip) printf "%s", content; inside = 0}' your-file


4

The Theory The rules are : inside a ' delimited string, nothing gets interpreted and anything but a ' doesn't have special meaning. This means that only a ' need escaping but it also mean that, in order to escape it, you need the '\'' construct. (The first ' ends the string, the following \' adds a literal ' (the escape prevents the start of a new string) ...


1

You can use -f option to make sed read command from file: sed -f delete.lineno.txt < my.file.txt


0

Don't use sed - it's not the tool for this job. Use ex, a superset of ed. tac delete.lineno.txt > cmds echo 'w!' >> cmds echo 'q!' >> cmds ex -s file < cmds Delete from the back of the file to avoid messing up the line numbering while you delete.


2

These are effectively multi-line records separated by a blank line. Awk is great for handling this kind of data: pactl list sink-inputs | awk -v RS="" '/VLC/' If you want to be really nit-picky about not including the bottom part of the record after the first occurrence of "VLC", then: pactl list sink-inputs | awk -v RS="" -v FS="\n" '/VLC/{ for(i=1; ...


3

Perl one-liner: perl -pe's/\[gene=([^\]]*)\K\]/$h{$1}++?"$h{$1}]":"]"/e' yourfile Explained: -p: execute the code for each line of the file (stored in $_) and print $_ at the end. -e: code. s/regex/replacement/e: Match regex and replace it with replacement on $_. The regex: /\[gene= # match [gene= ([^\]]*) # match anything but "]" and put it a ...


5

With ed: ed -s <<'IN' r !pactl list sink-inputs /VLC/+,$d ?Sink Input?,.p q IN It reads the command output into the text buffer, deletes everything after the first line matching VLC and then prints from the previous line matching Sink Input up to current line. With sed: pactl list sink-inputs | sed -n 'H;/Sink Input/h;/VLC/{x;p;q}' It appends ...


5

I'd use Perl's paragraph mode: pactl list sink-inputs | perl -00ne 'print if s/(.*?VLC.*?\n).*/$1/ms' The -00 sets the input record separator to \n\n so a "line" is a paragraph. Then, the substitution will match everything until the first VLC and then anything until the 1st newline and save them as $1. Everything after that is removed (since we're ...


0

According to explanation in comments: sed " # for lines which starts with if /^if\b/{ /==/! { # add logic statement to first alphanums after ( s/\((\w\+\)/\1 == 1b'1 / # add logic statement to second alphanums after & if it is present s/\(&\!\?(\?\w\+\)/\1 == 1b'1 / # if ! sign in section replace 1 by 0 ...


0

Maybe something like: perl -0777 -pe 's{\bif\s*\K\((([^()]++|\((?1)\))*)(?=\))}{ $& =~ s{([&(])\s*(!?)\s*\(?(\w+\b)\)?(?!\s*(==|'\''))}{ "$1$3 == 1'\''b".(0+!!$2)}gers}ges'


0

Just to be clear, are you using the "old awk" (/usr/bin/awk/) or the "new awk" (/usr/xpg6/bin/awk)? An online man page reference and GNU Awk's help page specifies the distinction quite clearly. Assuming if you are referring to "new awk" where you can use variable assignments via -v, you can also consider this: $ awk -v RS='name-' -v OFS='\t' 'NR>1{print ...


3

Here's another awk way (which, I now see, is just an uglier version of @Costas's): $ awk -F'[- ]' '($1~/name/){k=$2}($1~/school/){print k,$NF}' file JOHN NY TOM TX LILLY LA ROSY WA You can also use grep: $ grep -oP '^(name-\K\S+|school.*\s+\K.*)' file | paste - - JOHN NY TOM TX LILLY LA ROSY WA In your particular example, of course, you ...


5

By GNU sed sed -n '/^name-/{s///;N;s/[a-z].*\s//p}' file JOHN NY TOM TX LILLY LA ROSY WA By GNU awk awk -F'[ -]+' '/name/{a=$2}/state/{print a,$3}' OFS='\t' file JOHN NY TOM TX LILLY LA ROSY WA By grep grep -o '[[:upper:]]\{2,\}' file | paste - - JOHN NY TOM TX LILLY LA ROSY WA


2

awk '/name/ {gsub(/name-/,""); printf "%s\t",$1} /school/ {print $3}' file JOHN NY TOM TX LILLY LA ROSY WA


0

Using grep with PCRE : grep -P '^\"[^"]+\",\"[^@]+@[^.]+\.(?!br\")' file.csv Here we have used the zero width negative look ahead to ensure that .br does not appear in the mentioned place of the email address. Test : $ cat file.txt "foo bar","EMAIL@XYZ.com","foobar" "spam egg","EMAIL@XYZ.br","spamegg" "test new","EMAIL@ABC.com","testnew" $ grep -P ...


0

You need to escape the . so that it does not match any character to make sure it will not match something like "name@add.abr" for example. You can also look for .br that appears after an @. Try sed -i '/".*\@[^"]*\.br"/d' customer.csv Here is an example run: ~$ echo '"Phone Number","EMAIL@XYZ.com","NAME" > "Phone Number2","EMAIL@XYZ.br","NAME2" > ...


2

The common way to get a group of commands executed together (e.g., subject to a condition like a pattern match) is to group them with curly braces. With GNU sed: sed -n '/PostScript/{p; b}; /text/!p'


3

POSIXly: $ sed -n '/PostScript/p; //b /text/!p' POSIX define that if regular expression is empty, sed shall behave as if the last RE used in the last command applied. With your command, you need ; between p and b to make it work with GNU sed: $ sed -n '/PostScript/p; //b; /text/!p' With POSIX sed, you can't use //b; because ; is a valid branch name. ...


8

You have a space after \1 in your replacement, just remove that and you should be good perl -i -p -e "s/^(password[]*=[ ]*).*$/\1$passwd/" config.properties ^ Removed space here


0

If you don't need the email to be in a particular field of the .csv file, and only need to print those lines of the file that contain one of the target emails, you could just use grep: grep -wFf emails.txt file.csv > newfile.csv If they do need to be in a specific field, use @ChrisDown's solution. Note, however, that csv files can be quite complex. For ...


1

Assuming that the e-mails in X.txt are one per line, you can build an array, check it for existence, and then print out the matching lines: $ awk -F: 'FNR==NR { a[$2] = $0; next } ($1 in a) { print a[$1] }' customers.csv X.txt Foo Bar:foo@bar.com Baz Qux:baz@qux.com FNR==NR will only be true when in the first file. Here are the files I used to test this: ...


0

This is a lot easier with xml2: echo '<KeyValue key="EMPTY_SEARCH_CRITERIA" type="String" value="CS_CODE" comment="Configured empty search criteria."/>' | \ xml2 | \ sed '/@value=/s/=.*/=SN_CODE,CS_ID/' | \ 2xml


0

If you particularly want change the CS_CODE with SN_CODE,CS_ID you can use: 's/CS_CODE/SN_CODE,CS_ID/g' If the values differ you can use 's/value=\S*\S/value="SN_CODE,CS_ID"/g'


0

sed is not the brightest choice for html (or any other programming language) processing, but anyhow: sed -e 's/\(value\)="[^"]*"/\1="SN_CODE,CS_ID"/' Two perhaps not obvious points: with \( and \) we group a pattern to use it later on with \1 [^"]* is used to match any char but a "


0

Simply use awk. It's good for such tasks as selecting data based on columns: echo '3:ââsdb1 8:17 1 984M 0 part /media/(NAMEOFDRIVEHERE)' | awk '{print $7}'


1

At least with GNU sed: sed -i.bak '$a gem \x27forum2discourse\x27' file


4

sed -i '$a gem '"'"'forum2discourse'"'" Gemfile Alternate Solution If you wish to do it your way, then use the bash $'string' format. Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard. sed -i $'$a gem \'forum2discourse\'' Gemfile Source: ...


0

sed -i -e '$a gem '"'"'forum2discourse'"'" yourfile


0

I wrote the following script , It read dates.txt and convert all of date to EPOC(timstamp) and print them, Of course i wrote with bash , You need to change name of file and pipe output to a newlogfile: #!/bin/bash declare month=(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); while IFS= read -r line <&3; do sec1=`echo $line |awk -F/ '{print ...


0

Another way is using grep: grep -o -E '(/.*)$'


2

Using sed: sed -n '/media/{s#[^/]*/#/#;p;}' or awk: awk '/media/{print $NF}' or using lsblk itself: lsblk -nlo mountpoint | grep 'media'


2

With awk you can use print: awk '{ print "I am", $1, "of earth;" }' list or printf: awk '{ printf("I am %s of earth;\n", $1); }' list


2

Using sed: $ sed 's/.*/I am & of earth;/' file.txt I am john of earth; I am paul of earth; I am rose of earth; I am lily of earth;


3

Your problem comes from the .*. If you only match every character that is not a ' or a " it will work: sed -ri "s/\[ ([0-9]+|(\x27|\x22)[^\x27\x22]*(\x27|\x22)) \]/[\1]/g" file.php Even better (to take possible " or ' into account): sed -ri "s/\[ ([0-9]+|(\x27|\x22)[^\2]*(\2)) \]/[\1]/g" file.php


8

Inside [...], backslash is not special. [\[] matches both backslash and [1. If you want to include the ] character in the set, you have to make sure it's first: []X] matches ] or X while [X]] would match X followed by ] (and [X\]] would match X or \ followed by ]). To exclude it, it has to be right after ^: [^]X] is any character but ] or X. So, in your ...


0

(This overlaps somewhat with some of the other answers.) I’m somewhat confused.  You say, I want to add the contents of the firewall.txt file into the result.txt file before a line which is saved in search_string variable. OK, first of all, if they aren’t an essential part of the question, full pathnames (/root/…) clutter your question and add no ...


3

Use awk: awk '{a[NR]=$0} {if(NF!=1){y=$2;x=$0;b[x]=1;c[x]=1}else{b[x]++;if(y==$1){c[x]++}}} END{for (n in a){z=a[n];print z" "b[z]" "c[z]}}' file Admittedly it isn't the simplest one, but it works for me with your given input. Explanation: {a[NR]=$0}: First load all content of the file in an array called a. if(NF!=1): If it's a row where we have ...


0

sed '/\-A INPUT -s 192.168.0.0\/24 -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT/a CLIENTSCRIPT2="hello.sh"' file.txt In this case i added the variable CLIENTSCRIPT2="hello.sh"' on file.txt just under -A INPUT...etc Note the backslash escape in the syntax


0

sed has more commands than just s/// -- there's an r command to read another file into the current stream. However that reads the file after the current line, and you want to place it before the current line, so we have to juggle a bit. Note that you do not have to escape the slashes in the search string, we can use alternate delimiters in sed. $ cat ...


0

While this is likely possible with some sed calisthenics, I would use another tool instead. Perl, for example: search_string="\/sbin\/iptables -A INPUT -p tcp --dport 12443 -j ACCEPT"; delimeters=$(cat /root/firewall/firewall.txt); perl -i -pe "s/$search_string/$delimeters$search_string/" /root/result.txt


0

You can use sed to replace only the lines matching the particular pattern. sed -e '/typeId="ProxyService"/s/xmlns:imp="http:\/\/www.bea.com\/wli\/config\/importexport"/xmlns:imp="http:\/\/www.bea.com\/wli\/config\/importexport2"/' new.xml This will replace your namespace only in the lines matching "typeId="ProxyService" . I've just added a 2 to your ...


2

Here is one approach: awk 'BEGIN{FS="\t"} (($5!=0)+($6!=0)+($7!=0)+($8!=0))>=2' And a terser variant for the C hackers: awk -F $'\t' '(!!$5+!!$6+!!$7+!!$8)>=2'


0

replace the first occurence of >1 . by TAB: sed 's/\.\.\+/\t/' replace all occurences of >1 . by TAB:sed 's/\.\.\+/\t/g' / awk -F"[.][.]+" -vOFS="\t" '($1=$1)||1' replace the first occurence of >1 . followed by a digit by TAB: sed 's/\.\.\+\([[:digit:]]\)/\t\1/' replace all occurences of >1 . followed by a digit by TAB: sed ...


0

Use awk: awk '{print substr($0, index($0, $2))}' file Removes just the first field, but prints all other fields. Or sed: sed 's/Miriam132_38138 \+//' file If the > at the start of the line should be preserved, that removes just the first match.


0

If you want to remove first "word" from the line you can use something like awk '{print $2}' input.txt assuming you have whitespace between "words"


0

Assuming that the set of 7-9 characters ends with a space, and that Miriam is on the line only once: sed 's/\(.*\)Miriam.* /\1/' input.txt The \(.*\) part "saves" the pattern matched before Miriam and puts it back in with \1



Top 50 recent answers are included