-1

I have a large file containing data set names and dates/time:

ss:Type="String">EDII.HA5274.FNS99R.R999999.LFN5913.G1795V00</Data>
ss:Type="DateTime">2022-12-28T00:00:00.000</Data>
ss:Type="DateTime">1899-12-31T23:50:14.000</Data>
ss:Type="String">EDII.HA5273.FNS9901R.R999999.LFN5913.G1796V00</Data>
ss:Type="DateTime">2022-12-29T00:00:00.000</Data>also,
ss:Type="DateTime">1899-12-31T23:43:35.000</Data>

I want to concatenate the line following the word, "String".

My end result would be :

ss:Type="String">EDII.HA5274.FNS99R.R999999.LFN5913.G1795V00</Data>ss:Type="DateTime">2022-12- 28T00:00:00.000</Data>
ss:Type="DateTime">1899-12-31T23:50:14.000</Data>
ss:Type="String">EDII.HA5273.FNS9901R.R999999.LFN5913.G1796V00</Data>ss:Type="DateTime">2022- -29T00:00:00.000</Data>
ss:Type="DateTime">1899-12-31T23:43:35.000</Data>

My solution:

cat The_File.txt| sed 's/\(^.*String.*$\)\r\n/\1/g'

does nothing. "\r\n" are the correct eol characters. I can see them in my text editor.

I'm stuck now..

Any suggestions would be helpful. Also... what is wrong with my solution?

Thanks

3
  • You're working with some broken (or possibly purposely mangled) XML. It would be interesting to see what the original XML looked like and to know what you want to get out in the end (e.g. "get the DateTime value corresponding to some String value", or the other way around). That would allow us to suggest how to perform the task using some proper XML-aware tool.
    – Kusalananda
    Jan 21, 2023 at 0:58
  • I started with 200, 30000 line xml files.. My preprocess quick reduced this to one file with 9000 lines. Now I want to concatenate selected lines. I'm only inyerersted in using sed, ( or awk ). This works: sed '/"String"/{N;s/\r\n//;}' . But only on a unix machine, or when I run it from cygwin. It doesn't work in MacOs.
    – C0ppert0p
    Jan 21, 2023 at 18:32
  • Also, I don't have any xml aware tools, just Bash
    – C0ppert0p
    Jan 21, 2023 at 18:46

3 Answers 3

2

I'd use . If the line contains "String" print it without a newline, otherwise with one.

awk '
    BEGIN {FS = "\""; RS = "\r\n"}
    {printf "%s%s", $0, $2 == "String" ? "" : RS}
' The_File.txt
2
  • almost works. run as is, I got an "awk illegal statement at source line3". When I replaced "==" with "=" it worked.. sort of. it concatenated all lines after "String" I only wanted the first line following "String" to be concatenated.
    – C0ppert0p
    Jan 21, 2023 at 18:43
  • = is assignment. == is equality. With $2 = "String" the test becomes "is this an empty string", and it is not. Jan 21, 2023 at 22:07
2

By default, sed loads and processes a single line at a time.

To make it load a second line, you can use the N instruction. Then you can substitute the line ending character(s) with an empty string (or other joining string of your choice).

So

sed '/"String"/{N;s/\r\n//;}' TheFile.txt
3
  • This works great when I run it in cygwin on my windows machine, but does nothing when I run it in a bash shell, on MacOs. I also tried it using gsed. I thought it might have been the eol characters, so I substituted eol with a letter from one of the lines. Still nothing.. It's really irritating when MacOs has issues with simple bash commands
    – C0ppert0p
    Jan 21, 2023 at 18:26
  • hmmm did you mean "String" or String? my input file literally used the word "String". In any case my solution was to use gsed '/String/{N;s/\r\n//}', and to drop the ending ";". This works in MacOs. It may also work in Cygwin.. but I haven't tried it yet
    – C0ppert0p
    Jan 21, 2023 at 19:16
  • Ignore my (rolled back) edits. I misread your command.
    – Kusalananda
    Jan 21, 2023 at 19:41
-1

This works in MacOs:

gsed '/String/{N;s/\r\n//}'

This works in Cygwin bash on a windows machine, and also on linux machine:

sed '/"String"/{N;s/\r\n//;}' 
3
  • This seems to be duplicating an earlier answer. It would be best if you accepted that answer instead. See also unix.stackexchange.com/help/someone-answers
    – Kusalananda
    Jan 21, 2023 at 19:37
  • No, you are incorrect. In my case I have three environments, Windows with a cygwin shell, a Redhat Linux virtual machine, running on a Mac, and a Mac machine. The sed command works in the Linux environment, and the Windows/Cygwin/Bash shell. It does NOT work in in the OSX/Bash environment. The gsed command works in OSX. The sed command does not. Its subtleties like this, that make, our jobs interesting
    – C0ppert0p
    Jan 28, 2023 at 2:19
  • The substitution command in the gsed command is not terminated by a ; before the }, which is a non-standard way to write sed expressions only understood by GNU sed. Adding the missing ; to the command makes it work in both GNU sed and in more standard compliant sed implementations, like those found on macOS and other BSD systems. It is therefore slightly misleading to say that it depends on the operating system as GNU sed may well be installed as sed on macOS. Correcting the sed expression so that it is standard compliant will make it work in any sed implementation.
    – Kusalananda
    Jan 28, 2023 at 6:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .