LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Script that can extract information from lines of code in a txt file and place them in tables (https://www.linuxquestions.org/questions/linux-newbie-8/script-that-can-extract-information-from-lines-of-code-in-a-txt-file-and-place-them-in-tables-4175654706/)

KantieKay123 05-28-2019 12:21 AM

Script that can extract information from lines of code in a txt file and place them in tables
 
Hello,
i have lines of code that have parameters like description, capacity.
i want to have a script that can enable me place them in a csv file when they are delimited.

see sample code below
!
interface 11/4
description phone
capacity 5
color red
!

interface 11/4
description car
capacity 5
color red
!
interface 11/4
description house
capacity 5
color red
!

your help will be highly appreciated.

Turbocapitalist 05-28-2019 12:22 AM

Welcome.

That can be done easily with AWK or perl. What have you tried and where are you stuck? And for that matter, which distro (including version) is this for?

chrism01 05-28-2019 12:22 AM

Please show us what code you have done so far - we are here to help, rather than write it for you.

berndbausch 05-28-2019 01:15 AM

Quote:

Originally Posted by ruthkajobe (Post 5999592)
see sample code below
!
interface 11/4
description phone
capacity 5
color red
!

Read line by line. When you encounter an exclamation mark, output a new line. When not, output the line that you read followed by a comma.

This will leave you with a trailing comma, but perhaps that's not a problem. In the worst case, you can use sed to remove trailing commas in a second pass or in a second pipeline stage.

Pseudo-shell code:
Code:

while read LINE
do
    if LINE=="!"
    then echo
    else echo ${LINE},
    fi
done | sed '/,$/d'

Not sure if you want to do something special with the empty lines. Are they part of a CSV record, should they be ignored, something else...?

KantieKay123 05-28-2019 05:23 AM

Hello Guys,

thanks for the responses. I am new to scripting.
at the moment i would like to work with cygwin.

i would like an output that looks like below

interface description capacity color
11/4 phone 5 red
11/4 car 5 red
11/4 house 5 red

KantieKay123 05-28-2019 05:23 AM

interface description capacity color
11/4 phone 5 red
11/4 car 5 red
11/4 house 5 red

KantieKay123 05-28-2019 05:36 AM

what i want is to have interface description capacity color as column headers in a csv or excel file.

Turbocapitalist 05-28-2019 05:41 AM

Quote:

Originally Posted by ruthkajobe (Post 5999662)
what i want is to have interface description capacity color as column headers in a csv or excel file.

Ok. Then what you need is to try your hand at writing a script. We won't do your homework for you.
So the next step would be too look at awk or perl, or at the answer given in #4 above. Python would work, too, if you already know that.

It will be easier to work from a real distro on "bare metal", in order to keep legacy systems out of the way. For that you might look at one of the Ubuntu or Linux Mint variants. Try a few of the Live options and then when you decide which one is best, install that.

pan64 05-28-2019 05:45 AM

if I understand it well the operation you need is named transpose. you can find some tips for example here: https://unix.stackexchange.com/quest...of-a-text-file

KantieKay123 05-28-2019 07:20 AM

Thank you Pan 64. I can't wait to try the transpose solution.
I am going to try it out and will revert.

KantieKay123 05-29-2019 01:00 AM

hello

i have tried to run this script below, it works fine and returns for me expected result for only one row.

awk 'BEGIN { RS="descrption"} { gsub(/([A-Z]+=|;)/,"",$0) ; print $1 "," $2 "," $3 "" $4 "" $5 "" $6 "," $7 "" $8 "" $9}' tosr.txt >perfect.csv

am now stuck with getting it to output more than one row.
I have tried the while loop in vain

kindly assist.
Thanks

JJJCR 05-29-2019 02:23 AM

Try:

Quote:

while read -r line; do

awk 'BEGIN { RS="descrption"} { gsub(/([A-Z]+=|/,"",$0) ; print $1 "," $2 "," $3 "" $4 "" $5 "" $6 "," $7 "" $8 "" $9}' line >> perfect.csv

done < tosr.txt

Turbocapitalist 05-29-2019 07:33 AM

Or if the data is consistently starting each record with an exclamation point followed by one or more new lines, you coud adjust the Record Separator and Field Separator patterns for AWK:

Code:

awk '...whatever...' RS="\![\n]+" FS="[[:space:]]" OFS="," tosr.txt
Then use a comma or a tab for the Output Field Separator.

KantieKay123 05-30-2019 05:57 AM

Hi guys

thank you all for the support, my problem is solved.

awk 'BEGIN { RS="!"} { gsub(/([A-Z]+=|;)/,"",$0) ; print $1 "," $2 "," $3 "," $4 "," $5 "," $6 "," $7 "," $8 "" $9}' tosr.txt

works fine, it outputs all the rows.


All times are GMT -5. The time now is 04:18 PM.