1

How to turn this text:

                                             From: "anonymous" <sip:[email protected]>;tag=1c1969267918
                                             Call-ID: [email protected]
                                             Max-Forwards: 70
                                             From: "anonymous" <sip:[email protected]>;tag=1c6447025742
                                             Call-ID: [email protected]
                                             Max-Forwards: 70

Into this one:

From: "anonymous" <sip:[email protected]>;tag=1c1969267918, Call-ID: [email protected], Max-Forwards: 70
From: "anonymous" <sip:[email protected]>;tag=1c6447025742, Call-ID: [email protected], Max-Forwards: 70
1
  • have you looked at your post?
    – jsotola
    Jan 30, 2020 at 8:14

4 Answers 4

4

Unless the single space after the commas in your expected output are very important:

$ sed 's/^[[:blank:]]*//' file | paste -d, - - -
From: "anonymous" <sip:[email protected]>;tag=1c1969267918,Call-ID: [email protected],Max-Forwards: 70
From: "anonymous" <sip:[email protected]>;tag=1c6447025742,Call-ID: [email protected],Max-Forwards: 70

This first uses sed to remove the initial whitespace on each line in the input file, and then distributes the resulting lines, using paste, across three columns with commas as delimiters.

2

With awk:

awk '{sub(/^[[:blank:]]*/, ""); ORS = NR % 3 ? ", " : "\n"; print}' < your-file
0

If you want to do this in shell:

n=0
while read x; do
    echo -n "$x"
    n=$((n+1))
    if [ $((n%3)) = 0 ]; then
        echo
    else
        echo -n ', '
    fi
 done < inputfile

This read lines one by one, and keeps count. Every 3rd line (the $((n%3)) part calculates the modulus) a newline is output, otherwise the comma (and space as per your example) is output. The echo -n prevents echo adding a newline itself.

0

I believe it can be done more nicely. But it works fine.

gnu sed: First expression removes leading spaces. Second expression joins the following line matching /From:/ and removes leading spaces. Second expression joins the following line matching /Call-ID:/ and removes leading spaces.

sed -e 's/^[[:blank:]]*//' \
    -e  '/From:/N;s/\n[[:blank:]]*/, /' \
    -e  '/Call-ID:/N;s/\n[[:blank:]]*/, /'

You must log in to answer this question.

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