Tag Info

New answers tagged

0

{ paste -d\| /dev/fd/3 /dev/fd/4 | sed '/\([^ ]*\) [0-9:/ ]*\(.*\)|\1 .*\2/d;=' | sed 'N;s/\(\n\)\(.*\)|/:\tFILEA: \2\1\tFILEB: /' } 3<<\FILEA 4<<\FILEB qaqa rara abc 10:12:25 08/20/2014 123456 def ghi fff ddd jkl 09:20:40 08/20/2014 978645 dfdf gggg FILEA qaqa rara abc 10:32:15 07/15/2014 121456 xxx ghi eee ddd jkl 10:01:22 07/15/2014 ...


5

How about this command? csplit logname.log /---\ LOG\ REPORT\ ---/ {*} Testing cat logname.log --- LOG REPORT --- Mary Had A Little Lamb --- LOG REPORT --- Her Fleece Was White As Snow After running the above command, the output I get is, cat xx01 --- LOG REPORT --- Mary Had A Little Lamb cat xx02 --- LOG REPORT --- Her Fleece Was White As Snow


1

Wrong tool, use either perl or awk e.g. cat > splitFileByLogReport.pl <<EOF #!/usr/bin/perl undef $/; $_ = <>; $n = 0; for $match (split(/(?=--- LOG REPORT ---)/)) { open(O, ">$ARGV[$argnum]" . ++$n); print O $match; close(O); } EOF Then to run: perl splitFileByLogReport.pl yourFile.txt


4

How about something like awk '/--- LOG REPORT ---/ {n++;next} {print > "test"n".out"}' logname.log


0

The easiest command to get result would be the below $ diff <(tr -s "[0-9],:,/" " " < fileA) <(tr -s "[0-9],:,/" " " < fileB) The command is very straight forward and there is no complex regular expression as well. Sample output will be as below 2,3c2,3 < abc xxx < ghi eee ddd --- > abc def > ghi fff ddd Hope this is what ...


0

sed -n 'h;/match/n;G;P;//D' That will delete only the first match in any input file. It overwrites hold space with every line overwrites pattern space with the next input on match lines Gets hold space appended to patern space Prints up to the first occurring \newline character occurring in pattern space Deletes same on match lines The thing is ...


1

If your timestamps are consistently formated, you could strip them off (with sed, for example) before processing the files with whatever differencing method, e.g. diff <(sed -E 's|[0-9]{2}:[0-9]{2}:[0-9]{2} [0-9]{2}/[0-9]{2}/[0-9]{2,4} [0-9]{1,} ||' fileA) <(sed -E 's|[0-9]{2}:[0-9]{2}:[0-9]{2} [0-9]{2}/[0-9]{2}/[0-9]{2,4} [0-9]{1,} ||' fileB) ...


1

diff fileA fileB \ | egrep '^> ' \ | sed -r -e 's/^> //' -e 's\[0-9]{2}:[0-9]{2}:[0-9]{2} [0-9]{2}/[0-9]{2}/[0-9]{4} [0-9]{6} \\' To understand the above, read each section between the pipe | symbols separately. The first one simply creates a 'diff' output between the template and the data. Every line of the data file (fileB) that differs is output ...


0

It's the backslash. Backslash defuses the next character and makes it lose special meanings. The sed command is reading what you've typed as (just the sed expression here, I'm not doing the whole tedious shell command line): s <SPACE> C : Literal <SPACE> /root/ <SPACE> You've told sed that you want to use as the marker for the ...


2

Where you use: sed -i 's C:\ /root/ g' you're using the s command with a space character separating the different parts of the command, which is unusual, but completely valid. When you precede your separator character with a backslash, though, it's not treated as a separator, but as part of the argument itself. The problem you have here is that the ...


1

sed '/^<@\([^|]*\)\(.*\)>/!b s//\\autocite\2{\1}/ s/|/[/;s/|/][/g;/\[/s/{/]{/ ' <<\DATA <@sample> <@sample|12> <@sample|12|c.> DATA OUTPUT \autocite{sample} \autocite[12]{sample} \autocite[12][c.]{sample} The first thing sed does is verify the line it is working on begins with <@ followed at some point by a ...


2

You can use sed for this straightforwardly: sed -e 's/<@\([^|>]*\)|\([^|>]*\)|\([^>|]*\)>/\\autocite[\2][\3]{\1}/g' \ -e 's/<@\([^|>]*\)|\([^|>]*\)>/\\autocite[\2]{\1}/g' \ -e 's/<@\([^|>]*\)>/\\autocite{\1}/g' This just replaces each possible formulation separately: the first handles three-parameter citations, ...


0

You could do this: sed 'H;/^--$/h;/Content-Length.*[1-9]/!d;g' That appends every line to hold space. The -- marker lines overwrite the hold space - so you start with a fresh buffer for each of your -- through Content blocks. All lines that do not contain the string Content-Length followed at some point by at least one digit that is not a 0 are then ...


2

OK, found it. That now makes sense. the behaviour is explained at http://lwn.net/Articles/334756/ GNU and not GNU The default behavior of Nexenta is to prefer GNU utilities, which are installed in /usr/bin and /usr/sbin and so on. The Sun versions of these utilities are installed in /usr/sun/bin and /usr/sun/sbin. Nexenta uses a trick to be able to ...


2

Could also use awk Including match to end of file awk '/word/{x=1}x' file After first match til end of file awk 'x;/word/{x=1}' file


5

Including the last line you'd do: sed -n '/word/,$p' That matches the first occurrence of word all the way until the last line and prints all matches. Not including the last line: sed '/word/,$!d;$d' ...which deletes negated matches and then deletes the last line. And to get from only the last match to the last line you have to try a little harder: ...


0

Here's another portable way to do it with sed: sed 1i\\ file | sed '1,/^local/s/md5/trust/;1d' Just give yourself a little breathing room. That inserts a blank line at the head of the file so you can rely upon a thing or two. You might also do: { echo; cat file; } | sed '1,/^local/s/md5/trust/;1d'


0

{ rm -f -- "$1" && { printf '<?xml version="1.0" encoding="utf-8"?>\n<hello>\n\t<world>\n' paste /dev/null - printf '\t</hello>\n</world>\n' } > "$1" } < "$1" Would be more portable, efficient and readable, if not shorter.


2

In any case, sed "0,/^local/{s/md5/trust/}" Is GNU specific (the 0 address and the missing ; before }) and won't work with any other sed implementation (and Solaris doesn't ship with GNU sed by default). Portably/standardly: sed '/^local/,$!s/md5/trust/' to replace only on the lines up to (but not included) the first one starting with local. Or: awk ...


0

The question asks explicitly about sed, and it's a very valid question about sed regexp syntax. But in case the underlying question is about replacing spaces by _ really, here is an alternative answer using the "right tool for the job" for translating characters, which is tr. (man tr). The command tr abc 123 replaces the chars of the first list with the ...


2

You've already got an answer but I want to point out that in such simple scenario there is no need to be fancy: $ echo ' 1 2 3 ' | sed 's/ /_/g' _1_2_____3__ If you want to replace only spaces and tabs you can use [ \t] construct as well: $ echo -e " 1\t2 3 " | sed 's/[ \t]/_/g' _1_2_____3__


6

Because you use *, meaning it matches 0 or more spaces. So zero or multiple spaces is substitued by one underscore _. Try: $ echo ' 1 2 3 ' | sed 's/[[:space:]]/_/g' _1_2_____3__ Remember that [[:space:]] also matches tab, newline, carriage return. Note BREs Matching Multiple Characters


1

grep -oP 'name="\K[^"]*' filename Output: What_I_Want_To_Extract See: http://www.charlestonsw.com/perl-regular-expression-k-trick/ Your version adapted: grep -o 'name=".*">' HTMLFILE | sed 's/name="\|">//g' NEWFILE


1

To remove the first match(preserve line) awk '!x&&/99/{x++;$0=y}1' file To remove the first match(dont preserve line) awk '!x&&/99/{x++;next}1' file To replace first match awk '!x&&/99/{x++;$0="98"}1' file


4

You could use grep: string=testing && grep "$string" file.txt 2>&1 >/dev/null && echo "you search $string" The string "you search testing" is printed when the string "testing" is found somewhere in the file file.txt. string=testing: set the variable $string grep "$string" file.txt 2>&1 >/dev/null: grep searches for the ...


1

If I understand right, you want to remove only the first match, so you can do: sed -e '/99/{s//XXX/;:a;n;ba}' file With GNU sed, you can: sed -e '0,/99/s//XXX/' file


1

awk '/\r$/ {sub(/\r$/, ""); printf "%s", $0; next} {print}' file


3

^M is a Windows-specific EOL (End Of Line) and it consists of two characters: carriage return \r and new line \n. So you must include \n in your replace command as well: %s/\r\n//g If you want to stick with you original replace command, then you must first convert EOL-format of your file from Windows one to Unix one. You can use dos2unix tool to do that: ...


0

POSIXly: $ sed -e "/'test message1'/s//&\\ 'testing testing'/" file 'test message1' 'testing testing' 'test message2' 'test message3' 'test message4' 'test message5'


0

Awk solution awk -F ',' '{split($4, a, "-"); $4 = (a[3] "/" a[2] "/" a[1])}1' OFS=, file


2

This is what the a command does: sed -e "/test message1/a\\ 'testing testing'" < data This command will: Queue the lines of text which follow this command (each but the last ending with a \, which are removed from the output) to be output at the end of the current cycle, or when the next input line is read. So in this case, when we match a line ...


1

With sed: sed "s/'test message1'/'test message1'\n'testing testing'/g" Searches for 'test message1' Replaces with 'test message1' [new line] 'testing testing'. From this answer, you could also use: sed "/'test message1'/a 'testing testing'" Searches for 'test message1' Appends 'testing testing' after the matches.


0

Another awk solution awk -F, -va="([0-9]+)" '$0=gensub((a"-"a"-"a),"\\3/\\2/\\1","g")' file -F, Sets field separator as comma -va="([0-9]+)" Sets a variable(a) as the regex in the quotes. ([0-9]+) This regex matches any number of number in the range of 0-9(so all numbers really) and is within brackets so the match is saved. $0=gensub Sets $0(the ...


0

With sed: $ sed -e 's#\([0-9]\{1,\}\)-\([0-9]\{1,\}\)-\([0-9]\{1,\}\)#\3/\2/\1#' file abc,124,123,13/08/2014,abc def,124,123,13/08/2014,abc ghi,124,123,13/08/2014,abc If you have GNU or FreeBSD sed, you can use: sed -E 's#([0-9]+)-([0-9]+)-([0-9]+)#\3/\2/\1#' file or perl: perl -pe 's#(\d+)-(\d+)-(\d+)#$3/$2/$1#'


0

Perl solution: perl -aF'[,-]' -ne 'print join ",", @F[0..2], join("/", @F[5,4,3]), $F[6]' < input


2

An awk solution: awk -F'[,-]' '{printf "%s,%s,%s,%s/%s/%s,%s\n", $1, $2, $3, $6, $5, $4, $7}' file Explanation: -F'[,-]: set the delimiter to , or - '{printf "%s,%s,%s,%s/%s/%s,%s\n", $1, $2, $3, $6, $5, $4, $7}': print the part in the desired order and a newline at the end. And a sed solution: sed 's|\([0-9]*\)-\([0-9]*\)-\([0-9]*\)|\3/\2/\1|g' file ...


2

awk 'NR < 40000 {print; next} /\*\*STGN\*\*/ {line = $0; next} /\*\*IRV\*\*/ && line {print line; line=""} {print}' With sed: sed -e '40000,$!b' -e '/\*\*STGN\*\*/{h;d;}' -e '/\*\*IRV\*\*/{x;/./p;s/.*//;x;}' (they assume the STGN occurs before the IRV).


0

If you want to remove every number that comes before '=' awk -F= '{gsub(/[[:digit:]]/, X, $1)}1' OFS== file Above code satisfies even if there multiple numbers scattered before '=' hey1=lol h9e7y2=lol 1hey3=lol


0

Since you added a Perl tag: perl -pE 'BEGIN{ $/ = "<key>servers</key>\n<dict>\n"; $content = `cat file.xml` } $_.=$content' your_input_file


0

With another tool: strings $file


5

You can use sed: sed -e 's/[0-9]*=/=/' < data This replaces (s) any text that is zero or more characters in the range 0-9 followed by an = sign with just the = sign.


2

You're not providing a space getween the sed program and the filename set script1 to "sed 's@id_Shops@ShopID@g' " & lol # .......................................^


0

Could you be a little more specific on the result you expect ? I'd use python3 and a PATH = '/My/Path/' FILE = 'MyFile.xml' for i, line in enumerate(open(PATH+FILE, 'r')): ... # since it's easy to catch a \n, and a line ends with one, so finding the lines you're looking for is easy but I need a better understanding of the result to continue


1

Would something like this work? sed '\|<key>servers</key>|{n \|<dict>| r other-file.xml }' file.xml


1

sed '/keys_line_1/,/keys_line_last/{/keys_line_last/{ h;s/unique_split_point.*//;r /path/to/insert/file x;s/.*unique_split_point//;G }}' sed is not exactly forgiving when it comes to requiring adjustments to an hypothesis. Everything sed does is a direct result of the thing it has just done, and so a very minor error in detail can drastically alter ...


3

You can do something like: awk '{print} $0 == "<dict>" && previous == "<key>servers</key>" { system("cat other-file.xml") } {previous = $0}'


1

The other canonical tool you already use: grep: For example: grep -o 'stalled[^\n]*' Has the same result as the second option of Gilles: sed -n -e 's/^.*\(stalled: \)/\1/p' The -o flag returns the --only-matching part of the expression, so not the entire line which is - of course - normally done by grep. To remove the "stalled :" from the output, we ...


1

Attempting to actually answer the stated question: "Is there other ways that we can use to get the value without using $ sign." This isn't in the context of your use case, but you could do something like this: $SOME_VAR="some value or something" f() { echo ${!1}; } some_command `f "SOME_VAR"` You could use this to avoid using $ in the argument list for ...


0

You can use variables in sed as long as it's in a double quote (not a single quote): sed "s/$var/r_str/g" file_name >new_file If you have a forward slash (/) in the variable then use different separator like below sed "s|$var|r_str|g" file_name >new_file


4

As the final edits revealed, the problem is unrelated to the dollar sign, but is caused by the content of deststr, which is not 192.168.1.3 192.168.1.4 but rather two lines, one containing only 192.168.1.3 and the other containing only 192.168.1.4, both lines bveing terminated with a newline character. That is, the actual command after variable replacement ...



Top 50 recent answers are included