Trying to get this expression to work, can someone look at it and tell me how far off I am?
I’m trying to get info in line 1 to propagate to other blank lines.
Line 1 = "1234, type"
Line 2..99 = ", type"
Result: Line 1.. = “1234, type”
This is the expression
sed -i -R -n '{(^[^,]{});x;n;^,;s/^.{}/\1/;x;p;x;p}' SedExit.txt > Str.txt
Just in case you’re interested in an
awk-based solution, and assuming my interpretation of your request in my comment above is correct:EDIT: brief explanation –
-F,tells awk to use comma as the input field separator; it will automatically splitlines on comma and make the individual fields available as $1, $2, etc. ($0 is the whole line)
BEGIN {code}runs the code in braces once at the beginning of the awk run, before any input is processed.OFS = ","sets the output field separator to comma, to match the input. That lets us modify the fields directly and still get comma-separated output, instead of having to reconstruct the whole line in our code.The rest of the code is run once per line of input:
$1 { last = $1 }The first$1is a condition that must be met for the next block of code to be executed: if that field of the current line is true (non-empty), then the block is run. The block saves a copy of that field in a new variable called `last’.So if there’s anything before the first comma, we remember it for later use.
!$1 { $1 = last }The condition here is negated, so this block will only run if the first field is empty. In that case, we copy the other way, and set the first field to the value oflast. Hopefully we have previously seen a line that matched the first block and setlastto something, or this is not useful.{ print }There’s no condition here, so the code is run on every line, and it just prints it out. The output will incorporate our modification to $1, if applicable.