Tag Info

Hot answers tagged

8

There are four ways to include the single quote that you need. One cannot escape a single-quotes string within a single-quoted string. However, one can end the quoted string, insert an escaped single-quote, and then start a new single-quoted string. Thus, to put a single quote in the middle of 'ab', use: 'a'\''b'. Or, using the sed command that you need: ...


6

awk -F":" '{OFS=":"; print $1,$2,$3,$4-5,$5+5}' filename Output: 1314:Battery:1.90:45:35


6

sed delimits on \newlines - they are always removed on input and reinserted on output. There is never a \newline character in a sed pattern space which did not occur as a result of an edit you have made. Note: with the exception of GNU sed's -z mode... Just use tr: echo ls | tr -d \\n | xclip -selection clipboard Or, better yet, forget sed altogether: ...


5

sed ":a;/\r$/{N;s/\r\n//;b a}" This will match all lines that have '\r' at the end (followed by '\n'). On these lines it will first append the next line of input (while putting the '\n separator in between), then replace the resulting "\r\n" with an empty string, and then goes back to the beginning to see, whether the new contents of pattern space doesn't ...


4

perl -pe 's!.*(miR-\d+_microRNA).*(ENSG\d+).*!$1\t$2!' explain a bit? Sure, sorry: for each line (perl -p) substitute regex by $1 tab $2 where $1 and $2 are numbered backreferences to capturing groups (...). Nearly all modern regular expression engines support this. about the regular expression: . = any char except \n .* = a sequence of chars ...


4

Note that you don't have to read the file beforehand, sed has the r command that can read a file: $ printf -v var "%s\n" "s1random stuff" "s2 more random stuff" "s1 final random stuff" $ echo "$var" s1random stuff s2 more random stuff s1 final random stuff $ sed '/^s2/r file.txt' <<< "$var" s1random stuff s2 more random stuff line 1 line 2 s1 ...


4

The flags work together in the opposite way to what you're expecting. The documentation of /e is, for the record: This command allows one to pipe input from a shell command into pattern space. If a substitution was made, the command that is found in pattern space is executed and pattern space is replaced with its output. A trailing newline is suppressed; ...


3

With perl: perl -i -F: -lape '$F[3]-=5;$F[4]+=5;$_=join ":",@F' the-file With sh (assuming those numbers are always decimal integers without leading zeros): IFS=: read -r a b c d e < the-file && printf '%s\n' "$a:$b:$c:$((d-5)):$((e+5))" > the-file With recent versions of GNU awk: gawk -i inplace -F: -vOFS=: '{$4-=5;$5+=5}1' the-file


3

I can do it like: sed 's|\(,[^,]*,\)\{0,1\}\([^,]\{1,2\}\)|\1/\2|g ' <<\IN hello_hello,123-world567-helloworld123456,world1234-hello09876 IN ...which prints... /he/ll/o_/he/ll/o,123-world567-helloworld123456,/wo/rl/d1/23/4-/he/ll/o0/98/76 So most of the changes made are done to the second s///ubstitution - but ...


3

sed -e 's/& PID=\$\!;$//' The $ toward the end anchors it to the end of the string.


3

CentOS 7 specific: timedatectl | gawk -F': ' ' $1 ~ /Timezone/ {print $2}' And displaying just the timezone: timedatectl | gawk -F'[: ]+' ' $2 ~ /Timezone/ {print $3}' more generic: date +%Z


3

with awk awk '/P1/{a=1};a;/P2/{exit}' file something P1 something content1 content2 something P2 something


3

In sed: sed -n '/P1/,/P2/p; /P2/q' -n suppresses the default printing, and you print lines between the matching address ranges using the p command. Normally this would match both the sections, so you quit (q) when the first P2 matches. This will fail if a P2 comes before P1. To handle that case, try: sed -n '/P1/,/P2/{p; /P2/q}'


3

sed '/P1/,/P2/!d;/P2/q' ...would do the job portably by deleting all lines which do !not fall within the range, then quitting the first time it encounters the end of the range. It does not fail for P2 preceding P1, and it does not require GNU specific syntax to write simply.


3

You are getting multiple replacement, but you're don't get multiple executions. The pattern is executed once all the replacements have been made. Without the e flag the result of echo AAA | sed -r 's/A/echo B/g' is echo Becho Becho B So that's the command line that's executed if you do include the e flag, which is equivalent to echo 'Becho Becho B' ...


3

Many text processing tools, including sed, operate on the content of the line, excluding the newline character. The first thing sed does when processing a line is to strip off the newline at the end, then it executes the commands in the script, and it adds a final newline when printing out. So you won't be able to remove the newline with sed. To remove all ...


2

Using sed: sed 's/^.*\(miR-[0-9]*_microRNA\).*\(ENSG[0-9]*\).*$/\1\t\2/' <infile


2

Regarding your first query, you can use -e to combine expression in sed: ~$ sed -n -e '/^1/p' -e '/,,$/p' f 1, p1, p2, p3, p4, p5, p6, p7 2, p9, p10,, 2, p9, p10,, 2, p9, p10,, 1, p1, p2, p3, p4, p5, p6, p7 2, p9, p10,, and if you don't want the duplicate: ~$ sed -n -e '/^1/p' -e '/,,$/p' f | uniq 1, p1, p2, p3, p4, p5, p6, p7 2, p9, p10,, 1, p1, p2, p3, ...


2

Here is a sed solution that will string together any sequence of the ,,$ lines following the last occurring ^1 line: sed -e '/^1/{x;s/\n/ /gp;d' -e '};/,,$/H;$G;D ' <<\IN 1, p1, p2, p3, p4, p5, p6, p7 2, p9, p10,, 2, q1, q2, q3, q4, q5, q6, q7 2, q9, q10,, 2, r1, r2, r3, r4, r5, ...


2

Use Perl with lookaheads, so that the second comma is not part of the match: perl -pe 's/,(?=,)/,\\N/g' Or, use the same expression twice on the same line: sed 's/,,/,\\N,/g;s/,,/,\\N,/g'


2

Do it like this: sed -i "/$variable/c \\${variable}1" file.txt Changes: If you have a \ before a $, it makes the shell insert a literal $ in the string. Instead put an extra \ to escape the first one so that at literal \ will be printed. If your variable has letters or numbers immediately after it, enclose the name in {...} this distinguishes your ...


2

When you work globally you've got to consider how sed reads. A global substitution is going to divide up a pattern space into individual fields delimited per your specifications and operate on each. The delimiters are recognized from left to right - in the order they are read - and sed will apply each action as soon as it might. Here is a single ...


2

You can write it in much more readable form using awk: getval() { awk -F'=' '$1~/\<'"$1"'\>/{print $2}' testfile }


2

This is basic task for sed command: sed 's/AA\(someArbitraryString\)BB/CC\1DD/g' Eventually if you want to do this for all "arbitrary strings": sed 's/AA\(.*\)BB/CC\1DD/g'


2

That's typically where you'd use the hold space: ls | sed ' /\.png$/!d; # discard everything but lines ending in .png s///; # remove that .png h; # store on the hold space s/_//g; # remove underscores H; # append (with a newline) to the hold space g; # retrieve that hold space s|\n|/|; # substitute the ...


2

This is standard procedure to make sed non-greedy and back-reference tip for substring(s) in brackets () sed 's/\(\"[^",]\{1,\}\),\([^",]\{1,\}\"\)/\1 | \2/g' Or for GNU sed sed -r 's/("[^",]+),([^",]+")/\1 | \2/g'


2

If you are just putting commands in the clipboard echo -n "ls " | xclip -selection clipboard If you additionally need to make more complex transformations, echo "ls " | perl -pe 's/\n//' | xclip -selection clipboard


2

sed '$!N;/:.*\n$/d;P;D' <infile The above command pulls in the Next line on every line which is !not the $last. If it encounters a pattern space in which a : colon is found and the last character is a \newline it deletes the lot. This means that for lines which contain a colon and the following line is blank both will be deleted. For all others it ...


1

sed "s/\([AB]\)\1\1*/\n&\n/g s/AA\n\([[:alnum:]]\{1,\}\)\nBB/CC\1DD/g s/\n//g " <<\INPUT AAHelloBB Text AAByeByeBB INPUT I think that should only do the AA>>CC&&BB>>DD replacement if there is 1 or more alphanumeric character[s] between the two groups and should always squeeze the possible occurrences as near to each ...


1

With sed... getval() { sed "/^\([^=]*,\)*$1[,=]/!d;s/.*=//;q"; } <infile You might want to work on validating $1 as input though. Or with GNU grep and cut: getval() { grep -Em1 "^([^=]*,)*$1[,=]" | cut -d= -f2-; } <infile



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