Tag Info

New answers tagged


Parse the logfile line for line and suppress all \n's. When you see a new entry, first write \n except for the first time. You said Each entry has some stuff at the start (datetime, duration) but you did not give an example. Ok, I will call it NEW_ENTRY, you can modify. inStatement=0 cat logfile | while read -r line; do if [[ ${inStatement} = 0 ]]; then ...


Using gawk you could use a (subset of) PCRE expression as register separator (RS), define a different output register separator (ORS) and replace \n. Example: gawk 'BEGIN {RS="[ ]*;\n"; ORS="\n===\n"} {gsub("\n","\\n"); print} ' in this example: registers are separated by [ ]*;\n in the input registers are separated by "\n===\n" in the ...


Another way with sed that doesn't print empty lines: sed 's/|END|/\ /g;/^$/!P;D' infile e.g. input: T|one|two|END|T|three|four|END| T|five|six|END|T|seven|eight|END| T|nine|ten|END|T|eleven|twelve|END| output: T|one|two T|three|four T|five|six T|seven|eight T|nine|ten T|eleven|twelve same thing with ed: ed -s infile <<'IN' 1,$j s/|END|/\ /g ...


Another possibly awk command and using its RS option would be: awk '$1=$1' RS="\|END\|" file Will print those records (based on awk's Record Separator) which are not empty( has at least one field) to prevent printing empty lines. Tested on this input: T|somthing|something|END|T|something2|something2|END| Test|END| |END| Gives this output: ...


You can use awk: $ awk -F'\\|END\\|' '{$1=$1}1' OFS='\n' file T|somthing|something T|something2|something2 -F'\\|END\\|' set field separator to |END| OFS='\n' set ouput field separator to newline $1=$1 cause awk reconstruct $0 with OFS as field separator 1 is a true value, causeawk print the whole input line


The following worked fine for me: $ sed 's/|END|/\ /g' foobar T|somthing|something T|something2|something2 Notice that I just put a backslash followed by the enter key.


Use this: sed 's/|END|/\n/g' test.txt What you attempted doesn't work because sed uses basic regular expressions, and your sed implementation has a \| operator meaning “or” (a common extension to BRE), so what you wrote replaces (empty string or END or empty string) by a newline.


A couple of things could be happening. If the ^M is not actually the 2 charecters ^ and M, but is the way some editors can represent the Carriage Return (CR) character. eg. My Emacs editor shows it as such. This character is part of the end-of-line character pair in Windows file system: a Carriage Return (hex value 0x0D) + a New Line (hex value 0x0A). The ...


2 thoughts: with sed, for any line that ends with a carriage return, join the next line sed '/\r$/ {N; s/\r\n//} ' file with awk, define the record separator for input and output: awk -v RS='\r\n' -v ORS='' 1 file


Assuming that what shows up as ^M in your post really is a carriage return character (\r), the following should do the job: perl -pe 's/\r\n//g' It will work no matter how many lines your input contains: all lines ending with \r\n will be joined with the line that follows.


If it's really just three lines, and you always want to join the second and third line, you can use this: sed -e '2N' -e 's/\r\n//' the N command will add the next line (i.e. the third) to the second, and then the replacing will remove the linebreak.


^M looks for me like its a DOS file If ^M is really ASCII 13 (0x09) and not ASCII 94+77 (0x5E+0x4D) try to use: sed ':a;N;$!ba;s/^M\n//g' myfile.dat > mynewfile.dat

Top 50 recent answers are included