LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 05-28-2019, 12:21 AM   #1
KantieKay123
LQ Newbie
 
Registered: May 2019
Posts: 7

Rep: Reputation: Disabled
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.
 
Old 05-28-2019, 12:22 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,258
Blog Entries: 3

Rep: Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713
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?
 
1 members found this post helpful.
Old 05-28-2019, 12:22 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,348

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
Please show us what code you have done so far - we are here to help, rather than write it for you.
 
1 members found this post helpful.
Old 05-28-2019, 01:15 AM   #4
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by ruthkajobe View Post
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.
 
Old 05-28-2019, 05:23 AM   #5
KantieKay123
LQ Newbie
 
Registered: May 2019
Posts: 7

Original Poster
Rep: Reputation: Disabled
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
 
Old 05-28-2019, 05:23 AM   #6
KantieKay123
LQ Newbie
 
Registered: May 2019
Posts: 7

Original Poster
Rep: Reputation: Disabled
interface description capacity color
11/4 phone 5 red
11/4 car 5 red
11/4 house 5 red
 
Old 05-28-2019, 05:36 AM   #7
KantieKay123
LQ Newbie
 
Registered: May 2019
Posts: 7

Original Poster
Rep: Reputation: Disabled
what i want is to have interface description capacity color as column headers in a csv or excel file.
 
Old 05-28-2019, 05:41 AM   #8
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,258
Blog Entries: 3

Rep: Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713
Quote:
Originally Posted by ruthkajobe View Post
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.
 
1 members found this post helpful.
Old 05-28-2019, 05:45 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,685

Rep: Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274
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
 
1 members found this post helpful.
Old 05-28-2019, 07:20 AM   #10
KantieKay123
LQ Newbie
 
Registered: May 2019
Posts: 7

Original Poster
Rep: Reputation: Disabled
Thank you Pan 64. I can't wait to try the transpose solution.
I am going to try it out and will revert.
 
Old 05-29-2019, 01:00 AM   #11
KantieKay123
LQ Newbie
 
Registered: May 2019
Posts: 7

Original Poster
Rep: Reputation: Disabled
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
 
Old 05-29-2019, 02:23 AM   #12
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 2,141

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
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
 
1 members found this post helpful.
Old 05-29-2019, 07:33 AM   #13
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,258
Blog Entries: 3

Rep: Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713
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.
 
1 members found this post helpful.
Old 05-30-2019, 05:57 AM   #14
KantieKay123
LQ Newbie
 
Registered: May 2019
Posts: 7

Original Poster
Rep: Reputation: Disabled
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to import text out of .txt file and put/sort them in a libre office tables? webodin Linux - Newbie 4 03-11-2016 06:00 AM
cut first 10 lines of file master.txt and paste in ab1.txt and so on yogeshkumkar Programming 4 08-31-2011 07:23 AM
[SOLVED] Write a script to extract information from db tables LadyAnne Linux - Newbie 3 05-25-2010 05:02 AM
cat onelinefile.txt >> newfile.txt; cat twofile.txt >> newfile.txt keep newline? tmcguinness Programming 4 02-12-2009 06:38 AM
How can read from file.txt C++ where can save this file(file.txt) to start reading sam_22 Programming 1 01-11-2007 05:11 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration