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. |
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? |
Please show us what code you have done so far - we are here to help, rather than write it for you.
|
Quote:
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 |
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 |
interface description capacity color
11/4 phone 5 red 11/4 car 5 red 11/4 house 5 red |
what i want is to have interface description capacity color as column headers in a csv or excel file.
|
Quote:
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. |
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
|
Thank you Pan 64. I can't wait to try the transpose solution.
I am going to try it out and will revert. |
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 |
Try:
Quote:
|
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 |
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. |