[Help] format text file for import into SQL table (with sed or similar)
Hello everybody,
I am working on a project where I need to import gathered data into SQLite database table the textfile I am working with looks like this: Code:
NAME: "2811 chassis", DESCR: "2811 chassis" what I want it to look like Code:
2811 chassis|2811 chassis|CISCO2811|V06|XXXXXXXXXXX Code:
sed -e "s/^.*\"\(.*\)\".*\"\(.*\)\".*$/\1\|\2\|/" sample.txt it always starts with 'PID: ' then comes the word I want to extract (it never contains any space) then comes variable amount of spaces followed by (,) and another space after that always comes 'VID: ' followed by 3 characters I want to extract (it never contains any space) that is followed by ', SN: ' and a word I want to extract (it never contains any space) after that may or may not follow variable amount os spaces. then END-OF-LINE that is the structure of the file I need your help to process thanks you very much in advance. |
OK,
I kludged together a very ugly piece of code but it does the job done (at least partly) sed -e "s/^.*\"\(.*\)\".*\"\(.*\)\".*$/\1\|\2\|/" file.input | sed "s/ */ /g" | sed "s/SN: /|/" | sed "s/ , /,/g" | sed -e "s/^.* \(.*\),.* \(.*\)\,/\1\|\2/" | sed "s/ |/|/g" > file.output I had some problems because some files had some extra spaces but this one extracts all the data I need after executing this ugly long command the output looks like this Code:
2811 chassis|2811 chassis| Code:
2811 chassis|2811 chassis|CISCO2811|V06|FHXXXXXXX anybody that did not get a headache from my sed command willing to help? |
This looks like more of a job for sed and awk.
First thing I'd do is clean out all the junk in the input file with a file of sed editing instructions: Code:
cat sedit Code:
sed -f sedit sample.txt > junk.txt Code:
cat junk.txt Here's the awk program to read and format: Code:
cat sample.awk Code:
awk -f sample.awk junk.txt Code:
awk -f sample.awk junk.txt > sample.unl Hope this helps some. |
Thank you very much for your effort
it does the job quite well. I modified it a bit to also remove spaces before |. modified awk a bit so it works after this modification, added another variable to print and it does the job excellently. One huge thanks tronayne, have a nice day |
All times are GMT -5. The time now is 10:09 AM. |