Home > Back-end >  regex and parsing string lines
regex and parsing string lines

Time:04-28

I've the following file and I want to apply an regex to have 3 columns like in the below example:

file.log:

prometheus-1234 
51 /etc/prometheus
prometheus-2343242
51 /etc/prometheus
app32-ddsp8 
80 /usr/local/etc/app32
app33-dfssdx9 
68 /usr/local/etc/app33
proxy-dadas
87 /etc/hosts

And I'm trying to have the expected output from below but I didn't succeeded with awk or bash. expected output example:

prometheus-1234  51  /etc/prometheus
prometheus-2343242  51 /etc/prometheus
app32-ddsp8  80  /usr/local/etc/app32
app33-dfssdx9   68  /usr/local/etc/app33
proxy-dadas   87 /etc/hosts

I've tried this awk line:

parse.awk

BEGIN {
    RS="\n\n";
    FS="\n";

}
{
    print $1,$2;
}

awk -f parse.awk file.log
prometheus-1234  51  /etc/prometheus

It's output me just 1st 2 lines :)

CodePudding user response:

Using Awk:

Input:

$ cat test.txt 
prometheus-1234 
51 /etc/prometheus
prometheus-2343242
51 /etc/prometheus
app32-ddsp8 
80 /usr/local/etc/app32
app33-dfssdx9 
68 /usr/local/etc/app33
proxy-dadas
87 /etc/hosts

Output:

Assuming when line starts with numeric

$ awk '{printf("%s%s",$0,/^[0-9] /?RS:OFS)}' test.txt 
prometheus-1234  51 /etc/prometheus
prometheus-2343242 51 /etc/prometheus
app32-ddsp8  80 /usr/local/etc/app32
app33-dfssdx9  68 /usr/local/etc/app33
proxy-dadas 87 /etc/hosts

Assuming every 2nd line by modifying Output Record Separator(ORS):

$ awk '{ORS=NR%2?OFS:RS}1' test.txt 
prometheus-1234  51 /etc/prometheus
prometheus-2343242 51 /etc/prometheus
app32-ddsp8  80 /usr/local/etc/app32
app33-dfssdx9  68 /usr/local/etc/app33
proxy-dadas 87 /etc/hosts

CodePudding user response:

With your shown samples, please try following awk code. Written and tested in GNU awk.

awk -v RS= -v FS="\n" '{for(i=1;i<=NF;i =2){print $i,$(i 1)}}' Input_file

CodePudding user response:

I would use GNU AWK for this task following way, let file.txt content be

prometheus-1234 
51 /etc/prometheus
prometheus-2343242
51 /etc/prometheus
app32-ddsp8 
80 /usr/local/etc/app32
app33-dfssdx9 
68 /usr/local/etc/app33
proxy-dadas
87 /etc/hosts

then

awk 'NR%2{printf "%s ",$0;getline;print}' file.txt

output

prometheus-1234  51 /etc/prometheus
prometheus-2343242 51 /etc/prometheus
app32-ddsp8  80 /usr/local/etc/app32
app33-dfssdx9  68 /usr/local/etc/app33
proxy-dadas 87 /etc/hosts

Explanation: For every odd line (i.e. where remainder of divsion of row number by 2 is not zero) do printf current line so it is followed by space rather than newline, consume next line (getline) and just print it Disclaimer: this solution assumes that file has always even number of lines.

(tested in gawk 4.2.1)

  • Related