-1

so let's say i have

aaa
bbb
ccc
ddd

and i need to replace all new lines by comma and space, and end with a dot like so

aaa, bbb, ccc, ddd.

I found this, but i can't understand it at all and i need to know what every single character does ;

... | awk '{ printf $0", " }' | sed 's/.\{2\}$/./'

Can someone make those two commands human-readable ? tysm!

BenJ
  • 31
  • 5

5 Answers5

4

About the command:

... | awk '{ printf $0", " }' | sed 's/.\{2\}$/./'

Awk prints the line $0 followed by , without newlines. When this is done, you have , trailing at the end.

Then the pipe to sed replaces the last , with a single dot as this part .\{2\}$ matches 2 times any character at the end of the string.


With sed using a single command, you can read all lines using N to pull the next line in the pattern space, and use a label to keep on replacing a newline as long as it is not the last line last line.

After that you can append a dot to the end.

sed ':a;N;$!ba;s/\n/, /g;s/$/./' file

Output

aaa, bbb, ccc, ddd.
The fourth bird
  • 154,723
  • 16
  • 55
  • 70
0

ok, first of all; thank u.

I do now understand that printf $0", " means 'print every line, and ", " at the end of each'

as for the sed command, a colleague explained it to me a minute ago;

in 's/.\{2\}$/./',

s/ replace

. any character

{2} x2, so two characters

$ at end of the line

/ by ( 's/ / /' = replace/ this / that /)

. the character '.'

/ end

without forgetting to escape { and }, so u end up with 's/ . \{2\} $ / . /'

but wait, it gets even better;

my colleague also told me that \{2\} wasn't necessary in this case ; .{2} (without the escapes) could simply be replaced by

.. 'any character' twice.

so 's/..$/./' wich is way more readable i think

'replace/ wich ever two last characters / this character/'

hope this helps if any other 42 student gets here

tism again

BenJ
  • 31
  • 5
0
awk '{ printf $0", " }'

This is awk command with single action, encased in {...}, this action is applied to every line of input.

Printf is print with format, here no formatting takes places but another feature of printf is leveraged - printf does not attach output row separator (default: newline) as opposed to print.

$0 denotes whole current line (sans trailing newline).

", " is string literal for comma followed by space.

$0", " instructs awk to concatenate current line with comma and space.

Whole command thus might be read as: for every line output current line followed by comma and space

sed 's/.\{2\}$/./'

s is one of commands, namely substitute, basic form is

s/regexp/replacement/

therefore

.\{2\}$ is regular expression, . denotes any characters, \{2\} repeated 2 times, $ denotes end of line, thus this one matches 2 last characters of each line, as text was already converted to single line, it will match 2 last characters of whole text.

. is replacement, literal dot character

Whole command thus might be read as: for every line replace 2 last characters using dot

Daweo
  • 31,313
  • 3
  • 12
  • 25
0

Assuming the four lines are in a file...

#!/bin/sh

cat << EOF >> ed1
%s/$/,/g
$
s/,/./
wq
EOF

ed -s file < ed1
cat file | tr '\n' ' ' > f2
mv f2 file
rm -v ./ed1
petrus4
  • 616
  • 4
  • 7
-3
echo 'aaa
bbb
ccc
ddd' | 

mawk NF+=RS FS='\412' RS= OFS='\40\454' ORS='\456\12'
aaa, bbb, ccc, ddd.
RARE Kpop Manifesto
  • 2,453
  • 3
  • 11
  • 3
    How is providing a different way to do the same thing that is even hard to understand an answer to the question "can you explain what this means?"? – joanis Aug 30 '22 at 18:48
  • @joanis : because OP's code uses both `sed` and `awk` rather inefficiently – RARE Kpop Manifesto Aug 30 '22 at 18:55
  • 4
    OK, but then please take the time to explain your solution, I think OP will be just as lost here. – joanis Aug 30 '22 at 22:24
  • @joanis : nothing other than flattening the full input into a single row, and using `FS/OFS` to perform the typical functionality of `RS/ORS` – RARE Kpop Manifesto Aug 30 '22 at 22:58
  • @joanis : I initially assumed he was using octals for the old reserved ASCII file seperator and record seperator control characters, but the numbers are different. – petrus4 Sep 12 '22 at 11:37