Elias
Elias

Reputation: 41

Bash - How to format file?

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

Upvotes: 1

Views: 167

Answers (3)

Fravadona
Fravadona

Reputation: 17300

Given an aliase.txt like this:

Port_Alias "Aliasname1"  = 11, 12, 13
Alias  "Aliasname2" = 21, 22, 23 \
          24, 25, 26
Alias  "Aliasname3" = 31
H_Alias  "Aliasname4" = 41, 42, 43 \
          44, 45, 46
Alias  "Aliasname5" = \
51 \
    52 \
        53
  Alias "Aliasname6"=61

The following awk program:

awk '
    $1 == "Alias" {
         while (sub(/\\$/,""))
             getline $(NF+1)
         gsub(/[ ,"=]+/," ")
         for (i = 3; i <= NF; i++)
             print $2, $i
    }
' aliase.txt

Will yield:

Aliasname2 21
Aliasname2 22
Aliasname2 23
Aliasname2 24
Aliasname2 25
Aliasname2 26
Aliasname3 31
Aliasname5 51
Aliasname5 52
Aliasname5 53
Aliasname6 61

Upvotes: 1

RARE Kpop Manifesto
RARE Kpop Manifesto

Reputation: 2925

echo "${input…}" | 

mawk  NF=NF  RS='(\\?(\r?\n)+)?(H_|Port_|)Alias |\\?\n+'\
     OFS='|' FS='[^[:alnum:]]+'  
|Aliasname1|11|12|13|
|Aliasname2|21|22|23|24|25|26|
|Aliasname3|31|
|Aliasname4|41|42|43|44|

From here on it should be mostly trivial regarding the final formatting.

Maybe these staircases would help :

mawk '(OFS="\f\b\b\b\b\b\b\b\b\b\b\b\b"$(++_+_)" "\
       )^!(NF-=_*($++_=$--_=_="")^_)' \
   ORS= \
    FS='[^[:alnum:]]+' OFS= RS='(\\?(\r?\n)+)?(H_|Port_|)Alias |\\?\n+' 
Aliasname1 
Aliasname1 11
 Aliasname1 12
  Aliasname1 13
   Aliasname2 
  Aliasname2 21
   Aliasname2 22
    Aliasname2 23
     Aliasname2 24
      Aliasname2 25
       Aliasname2 26
        Aliasname3 
       Aliasname3 31
        Aliasname4 
       Aliasname4 41
        Aliasname4 42
         Aliasname4 43
          Aliasname4 44

Upvotes: 0

Shawn
Shawn

Reputation: 52674

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)

Upvotes: 1

Related Questions