text into csv conversion

Red hat Red hat linux 7.1
February 17, 2011 at 02:53:39
Specs: Linux i686, 256
thank u for allwing me to join your forum

i have a text file in thie following format.................


PASS_MAX_DAYS 99999
PASS_MIN_DAYS 0
PASS_MIN_LEN 5
PASS_WARN_AGE 7
PASS_CHANGE_TRIES 5
PASS_ALWAYS_WARN yes
GETPASS_ASTERISKS 1

how to convert this into some format like
pass_min_days, pass_min_length,Pass_warn_age ...........
5 7 yes

i want the output in csv format for database import

my mechine is linux and how to do this using bash script.........

any idea lease
thanks in advance


See More: text into csv conversion

Report •

#1
February 17, 2011 at 08:40:43
I am having trouble determining your data format, Do you want everything on one line where column 1 is listed first seperated by commas follwed by column 2 seperated by commas? Everything downshifted?

Report •

#2
February 17, 2011 at 18:14:03
yes....all columns are in one line seperated by commas.. and next columns of results are downshifted ..............for example

pass_min_days, pass_min_length,Pass_warn_age,pass_age_tries,pass_al_warn
0 , 5 , 7 , 5, yes

please help
thanks in advance


Report •

#3
February 18, 2011 at 13:15:48
I choose to preprocess the file first: change the entire file to lowercase, eliminate the two lines not needed and determine the length of the file.

The awk script prints out the first column, saves the second column in array twocol, and prints out the twocol array when the process ENDs.:

#!/bin/bash

# downshift the data. assumes data file contains no blank lines
tr '[:upper:]' '[:lower:]' < data > newdata

# eliminate the lines you don't need
sed -e '/pass_max_days/d' -e '/getpass_asterisks/d' newdata > mynewdata

# get the number of lines in the new file
numlines=$(wc -l < mynewdata)

awk ' {

# save the 2nd col
twocol[NR]=$2

str=$1
# override str variable if needed
if($1 == "pass_always_warn")
   str="pass_al_warn"


if(NR < len)
   printf("%s,", str)
else
   # no comma
   printf("%s ", str)

}
END { for(i=1; i <= len; i++)
            if(i < len)
               printf("%s,", twocol[i])
            else
               printf("%s ", twocol[i])
}
' len="$numlines" mynewdata
# end script

Some of your output columns are different from the input file.  I only changed pass_always_warn.  I leave changing the others to you.
 


Report •
Related Solutions


Ask Question