[SOLVED] Script that can extract information from lines of code in a txt file and place them in tables
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
!
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?
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...?
Last edited by berndbausch; 05-28-2019 at 01:16 AM.
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.
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:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.