# Tag Info

1

0

This works on your example input line: sed 's/.* $$[0-9].*)$$ .*>/(\1: RAW DATA OUT:/ ' <<\DATA 1: (10/17 12:49:31.175) - CONSTANT ID1 - CONSTANT ID2: RAW DATA OUT > [0x00,0xa2,…,0x00] DATA It prints: (12:49:31.175): RAW DATA OUT : [0x00,0xa2,…,0x00]

1

You can also call perl from bash. The -n arg makes it loop for each line. -e means the script is one line. cat in.txt | perl -ne 's{.*(.*$$).*? (.*) -.*(:.*?)\s\s+.*(\[.*)}{123 : 4};' 0 sed ' s/^[^(]*// s/([^ ]\+ /(/ s/ - CONSTANT ID// s/ \+>/ :/ ' <<DATA 1: (10/17 12:49:31.175) - CONSTANT ID: RAW DATA OUT > [0x00,0xa2,…,0x00] DATA (12:49:31.175): RAW DATA OUT : [0x00,0xa2,…,0x00] The newlines in the sed script can be replaced by semicolons if you prefer In a while-read loop: while read line; do echo ... 0 The two commands produce the same output only for your input but otherwise they are different. For understanding of what is going on we have to know how is the parameter interpreted first by bash and then by grep. Escaping in bash \ is a special character which cancels special meaning of the following character including \ itself. If the following ... 4 The output is the same only for your string, but in general those regular expressions do different things. Let's modify your example a little by adding second pattern e,g, (with comas), third e\.g\. (dots), fourth e\,g\, (comas), and -o option to grep to print only matched parts. In the following case . match any char (notice '' around e.g., I will come to ... 3 When you do a grep e\.g\., the shell is consuming the backslash, thus you are doing a grep e.g., which matches. When you do a grep e\\.g\\., the shell is again consuming a slash, and now you are doing a grep e\.\g., which again matches. Now, a backslash to the shell looks like \\. So, when you have \\, the first one is an escape sequence, the second is a ... 8 First, note that the single slash matches too much:  echo 'eegg \n e.g.' | grep e\.g\. eegg e.g. As far as Bash is concerned, an escaped period is the same as a period. Bash passes on the period to grep. For grep, a period matches anything. Now, consider:  echo 'eegg \n e.g.' | grep e\\.g\\. e.g.  echo 'eegg \n e.g.' | grep e\\\.g\\\. e.g.  ... 5 This regular expression will match HTML comments In particular: <!-- matches literal string "<!--" . matches any character * is a quantifier, it means "0 or more" of the previous character ? makes the regex non-greedy, so it matches as few times as possible --> matches literal "-->" So, your regexp against this text: blah <!-- ... 3 <!-- matches the characters <!-- literally . matches any character (except newline) *? is a quantifier that matches the preceding regex between zero and unlimited times, as few times as possible, expanding as needed [lazy] --> matches the characters --> literally Answer from the regex tester http://regex101.com/r/lA1bH5/2 Note that * matches ... 2 To replace first 10 digits with * if, and only if, the number has exactly 14 digits: sed 's/\([^0-9]$$[0-9]\{10\}$$[0-9]\{4\}$$$$[^0-9]\|$$/\1**********\2\3/g' Example: $echo 'foo bar 38012345678901 2014-02-11 22:23, 1134-53553, 4-5-6-7-7-2, 28012345678901,,,,, 3801234567890123456789 stuff' | \ sed ... 0 The error is telling you that your sed command is wrong, not your regexp. You need a newline or semicolon to separate the s command from the following { command. Equivalently, you can put them in separate -e arguments. sed -e 's/.SRC="/Repository([^"])".*/\1/p' -e '{' -e 'r File1' -e 'd' -e '}' File2 This won't do what you want, though. It removes the ... 0 I came up with a few ways to do this in sed, but most broke in corner cases. One, however, doesn't: sed 's/^/\n/;:b /\n\n/!s/$$\n[^,{}]*$$,/\1|/;tb s/$$\n\n*$$$$[^{}]*[{}]$$/\2\1/ s/{$$\n$$/&\1/;s/$$}\n$$\n/\1/;tb s/\n//g' <<\DATA (999969,2500,"777777888",0,"45265","65522",NULL,10001,2014-09-15 10:27:07.287,2014-09-15 10:28:49.085,2014-09-15 ... 3 If you are trying to replace in File1 everything inside double quotes with new image names taken from File2 then I would use awk: awk -F'"' 'NR==FNR{a[i++]=$1;next}{print $1 FS a[j++] FS$3}' File2 File1 The output is the following: <IMG SRC="/getimage.dll?path=Orange/2011/03/27/129/Img/Ad1291103.gif" /> <IMG ...

1

I have no idea what you're trying to do there but then my sed-fu is not that strong so I guess you're using some arcane syntax I am unaware of. Since I can't tell you what's wrong with your sed (but an educated guess is that the special characters contained in your replacement strings (/,? etc) are causing problems), I will instead offer a perl alternative: ...

3

You could do this simply through Perl+regex combination. perl -pe 's/(\{(?:[^{}]|(?1))*\})(*SKIP)(*F)|,/|/g' file Example: $perl -pe 's/(\{(?:[^{}]|(?1))*\})(*SKIP)(*F)|,/|/g' file (999969|2500|"777777888"|0|"45265"|"65522"|NULL|10001|2014-09-15 10:27:07.287|2014-09-15 10:28:49.085|2014-09-15 ... 0 Don't be afaid it's a hard sed statement. But, it should respect cascading. Here is it in one line: sed -e 's/,/|/g;:a;s/{$$[^{}]*$$|$$[^{}]*$$}/{\1,\2}/g;ta;s/{$$[^{}]*$$}/<\1>/g;ta;:b;s/<$$[^<>]*$$>/{\1}/g;tb' file Here is the commented version: sed -e ' s/,/|/g; #replaces all commas (,) with ... 0 replace ,{ with |{ and }, with }| echo "THING1,{THING2,{THING3,}},THING4" | sed -re "s/,\{/|{/gi" | sed -re "s/},/}|/gi" results in THING1|{THING2|{THING3,}}|THING4 1 Preceding the first regexp with a space: grep -E "\s+-?[[:digit:]]{1,3}\.[[:digit:]]{0,5}" should do the trick, since it excludes the match at the beginning of the line. If you want just the 4th column, you can easily achieve that with either GNU sed: sed -r 's/^\S+\s+\S+\s+\S+\s+(\S+)(\s.*|)$/\1/' where \s is any whitespace character (space, tab and ...

4

Anchor the expression to the end of the line: grep -E "\-?[[:digit:]]{1,3}\.[[:digit:]]{0,5}$" If you add the PCRE option of -o to return only the captured group, you will see that your example matches and returns the desired match: grep -oE "(\-?[[:digit:]]{1,3}\.[[:digit:]]{0,5}$)" -146.17516 Too, to catch matches for lines in a file with optional, ...

1

You can try a loop in sed: $sed -n '{:loop p; n; /SOMETHING/b loop; q}' test.txt blah blah SOMETHING blah blah blah blah SOMETHINGblahblahblah SOMETHING blah blah This: :loop creates a label named loop p prints the current line n fetches the next line /SOMETHING/b loop branches to loop if line matches /SOMETHING/ q if the branch doesn't happen. This ... 0 This might not be as general as what you really want, but here’s a starting point: sed -n '/SOMETHING/!q;p' This says: check for match to /SOMETHING/. If the line doesn’t match (using ! to invert the result of the test), then quit. Otherwise, print this line and continue to the next line. This is not immediately flexible enough to allow you to do what ... 1 In double quotes, you need to backslash backslashes, i.e. double the backslash before the dot. system("grep '^.*\\.[a-zA-Z0-9][a-zA-Z0-9]*\$' file.txt > file2.txt"); # ^ # | # Here.

1

Do you need it to be a pure regex solution, or just perlish? perl -lne 'print if(/^\\begin{question}/ .. /^\\end{question}/)' file

3

Try this: pcregrep -M '\\begin{question}(.|\n)*?\\end{question}' Explanation: pcregrep: grep with Perl-compatible regular expressions -M: Allow patterns to match more than one line (.|\n)*?: any normal character . or new line \n matched zero or more times ., in non-greedy mode ?. Result: \begin{question} {You get a patient. What do you notice ...

2

If I understand correctly, you're looking for something like this (in bash): #!/usr/bin/env bash ## avoid errors if a directory has no *tex files shopt -s nullglob directories=("Cardiology" "Rheumatology" "Surgery"); ## Change this to set whichever options you want. printf "%s\n%s\n" "\documentclass{YOURCLASS}" "\begin{document}" for directory in ...

0

If for some reason you cannot use single quotes as suggested in Mikel's answer, you can temporarily turn off history expansion using set +H (turn it back on with set -H), as suggested by glenn jackman in comments.

0

As @l0b0 said, I don't think this can be done in a single step in the shell but it's simple enough to do with external tools (this assumes the string is saved in the variable $s):$ perl -pe 's#[.\-/]##g' <<<$s | grep -oP .{16} mysitesubdomainc 2 Why not use zsh?$ echo ${${mysite//[\.\/-]/}:0:16} mysitesubdomainc (I slightly modified you code)

3

There's no way to chain the Bash built-in parameter expansion, but of course this can be done in a single line with external tools like sed: \$ sed 's/[\.\/-]//g;s/^$$.\{16\}$$.*/\1/' <<< "./my-site.sub.domain.com" mysitesubdomainc Unfortunately this very quickly turns into unmaintainable code, and is probably less efficient than using Bash ...

Top 50 recent answers are included