Home > Mobile >  Bash - How to format file?
Bash - How to format file?

Time:07-14

I have a large file called aliase.txt, and this files contains a list of some alias. They have this syntx: Alias "Aliasname" = 1, 2, 3.

But sometimes they have the syntax:

Alias  "Aliasname" = 1, 2, 3 \
      4, 5, 6

Sometimes it has more than one new line, and there are more Backslash. but these are also offset by 10 spaces. (The new line is always offset by 10 spaces.)

I need to get rid of the backslash and the "return", so that it would look like this: Alias "Aliasname" = 1, 2, 3, 4, 5, 6. In the End this should become a new file, I can send to someone.

After that I am supposed to format the file even more, so that in the End, it would look like this:

Aliasname      1
Aliasname      2
Aliasname      3
Aliasname      4
Aliasname      5
Aliasname      6
Aliasname2      1
Aliasname2      2
Aliasname2      3

But this is not the only content in the file, there are some other alias too. For example: H_Alias or Port_Alias. But these are not important for me, I only need to filter out the alias: Alias. I already filtered out comments and Blank Spaces.

Is there a way this is possible, and if there is, could someone please help me with it.

Kind Regards Elias

CodePudding user response:

If your file actually looks like this:

Alias "Aliasname1" = 1, 2, 3
Alias "Aliasname2" = 4, 5, 6 \
          7, 8, 9
Alias "Aliasname3" = a, b, c

you can join the continued lines with:

sed '/\\$/{N; s/\\\n[[:space:]]\{10\}/, /}' aliase.txt
Alias "Aliasname1" = 1, 2, 3
Alias "Aliasname2" = 4, 5, 6 , 7, 8, 9
Alias "Aliasname3" = a, b, c

(If a line ends in a backslash, read the next line of input and append it to the pattern space, then replace the sequence "backslash, newline, and 10 more whitespace characters" with a comma)

CodePudding user response:

Using sed

$ sed '/^Alias.*\\$/{N;s/ \\\n \ /, /}' aliase.txt
Alias  "Aliasname" = 1, 2, 3, 4, 5, 6
  • Related