New answers tagged

0

The perl cookbook has an example of how to do this using perl. I've adapted that example to work with <> (stdin and/or whatever filename(s) are given as args on the command line) rather than a named file-handle, and also to retain the newline after the \ continuation (which is slightly unusual - it's far more common to want a continued line to be ...


2

With posix awk you can use getline awk '{while(/\\$/){getline tmp;$0=$0"\n"tmp}print "<LINE>"$0"<LINE>"}' file Just keeps adding the next line as long as the last line end in \. <LINE>entry1line1<LINE> <LINE>entry2line1\ entry2line2\ entry2line3<LINE> <LINE>entry3line1<LINE> Same in perl perl -ne ...


1

Using GNU awk: $ awk '{printf "%s%s%s","line=",$0,RT}' RS='[^\\\\]\n' text line=entry1line1 line=entry2line1\ entry2line2\ entry2line3 line=entry3line1 As you can see, the lines ending with \ are joined to the next line. This is because the record separator RS was redefined to be any non-backslash followed by a newline. In other words, backslash-newline ...


0

You can change the record-separator in POSIX awk by changing RS. POSIX does not specify whether that can be a regular expression, saying only The unspecified behavior from using multi-character RS values is to allow possible future extensions based on extended regular expressions used for record separators. Historical implementations take the first ...


0

Consider this sample file: $ cat sample.html <li a=x>Point One </li> <li>Point Two </li> I believe that this sed command does what you ask (this may require GNU sed): $ sed -Ez 's|<li\b|\t<li|g; s|\n</li\b|</li|g' sample.html <li a=x>Point One</li> <li>Point Two</li> How it ...


0

OK, so the best solution for me (but I guess it's a matter of taste), was using PHP's fgetcsv, since I already have PHP on that server. It's a pity the bash read command doesn't handle the newlines as well as the PHP function. It automatically recognises additional delimiters (like "'s). Sample: <?php $row = 1; if (($handle = fopen("test.csv", "r")) !== ...


1

You should really be using a proper CSV parser. For example, using the one that ships with ruby: ruby -rcsv -e 'CSV.foreach("file", :col_sep => "|") {|row| p row; puts row[1]}' we get ["aaa", "111", "!!!", ""] 111 ["bbb", "222", "@@\n@", ""] 222 ["ccc", "333", "###", ""] 333 You can see for the 2nd row, there is the embedded newline. Remove p row to ...


3

For a shell with a read builtin that can handle CSV, you can use ksh93 instead of bash: $ while IFS='|' read -rS a b c; do printf '%s\n' "$b"; done < file 111 222 333 To convert that format to something that bash's read can handle, you could do: < file ksh93 -c 'while IFS="|" read -rSA a; do printf "%s|" "${a[@]//[\|]/\\\0}" ...


2

There are carriage returns at the end of $i, because there are carriage returns at the end of the lines in all.txt. It was probably produced on Windows: Windows uses the two-character sequence CR,LF to mark a line break, whereas Unix (and most of the rest of the world) uses just LF (linefeed, synonym of newline in the Unix world), so Unix sees a line with a ...


-1

Your error message indicates that sed is getting an argument of /log.dat - neither $i or $in are set to any value. I think you have a blank line in your All.txt file. /log.dat: No such file or directory #(then the path to this file)



Top 50 recent answers are included