Operating System - HP-UX
1830350 Members
2528 Online
110001 Solutions
New Discussion

Spliting and merging files

 
SOLVED
Go to solution
jayachandran.g
Regular Advisor

Spliting and merging files

Hi all

i have got a output of counters in the following format
Counter = 12
coun = 234

now i want to open the file in Microsoft excel, sheet the problem is excel sheet need a tab difference to recognize the difference between any two words otherwise it will think the whole as a single word.

and cut command is also failed to distinguish the counter and values as fields it is thinking it as a single filed.

how to proceed now?

There e are lot of counters like this and values will continuously increasing.

From my view i think easiest way is to cut the alphabets and numerical separately merging it with a tab space.

plz help my providing a solution
11 REPLIES 11
Pat Lieberg
Valued Contributor
Solution

Re: Spliting and merging files

If I understand correctly, you want to put the words into one field and the numbers into the next field. What you can do prior to opening the file in Excel is replace the " = " with a comma (,). Excel should be able to use the comma seperated file to properly populate the fields in your spreadsheet.
Simon Hargrave
Honored Contributor

Re: Spliting and merging files

A couple of options: -

Excel can import with any field separator, so you should be able to select "space" as a field separator.

For cut, you can use the option -d" " to set "space" as a delimiter.

Or you can use awk which by default will allow any whitespace (space, tab etc)

awk '{print $1,$3}' filename
TwoProc
Honored Contributor

Re: Spliting and merging files

As Pat indicated using a comma in plain text files works great as field separators in Excel. It's how we get data for the spreadsheet folks extracted from the database, and it works fine.
We are the people our parents warned us about --Jimmy Buffett
Juan M Leon
Trusted Contributor

Re: Spliting and merging files

jayachandran,
Let me understan better, the entire file is one colum with counter=number? correct? and also it has many lines with the same format the difference is that some are Couunter and some are count, correct?

if that is correct you can try the follwiing

cat | while read x
do
echo "$x \n" >> newfile
done


hope it helps
Mel Burslan
Honored Contributor

Re: Spliting and merging files


DATAFILE=/path/to/my/datafile
NEWFILE=/path/to/tab/delimited/file
COUNTERS_PER_LINE_in_EXCEL=15 # modify this as you wish

cell_ctr=0

cat $DATAFILE|while read a b c
do
printf $c"\t" >> $NEWFILE

(( cell_ctr=$cell_ctr+1 ))
if [ $cell_ctr -gt $COUNTERS_PER_LINE_in_EXCEL ]
then
printf "\n" >> $NEWFILE
cell_ctr=0
fi

done


________________________________
UNIX because I majored in cryptology...
Juan M Leon
Trusted Contributor

Re: Spliting and merging files

Ok I was confused with the question. what I suggested before is not going to helps you. I incline to use Simon idea but add somethin else at the end of his line

awk -F= '{print $1","$2}' filename > newfilename
jayachandran.g
Regular Advisor

Re: Spliting and merging files

Hi All

Thanks for all u'r valuble response.
Pat you have simply slapped me.. with your command, it is excellent. but comma (,) did not work i don't know exactly the reson but insted of comma(,) i have inserted a tab space
:%s/=/ /g
and it done the magic. now even cut command is looking it as a seperate field with may help me when i have same counters from different machines to merge in the same file.
jayachandran.g
Regular Advisor

Re: Spliting and merging files

Yaa i got to the loop now
i have two files
one
filed1 field2
counter 3
count 3

another file

filed1 field2
counter 6
count 7

now i want to do

file1 file1 file2
field1 filed2 field2
counter 3 6
count 3 7

note the counter and its values are not the same.
Sandman!
Honored Contributor

Re: Spliting and merging files

Jayachandran,

The default delimiter for cut is therefore you need to specify the one you need to use with the "-d" option i.e.

# cut -d" "

In your case it shoud be:

# cut -d"=" -f filename
jayachandran.g
Regular Advisor

Re: Spliting and merging files

I have tred the psste command but it failed.
paste file1 file2
output was like this
ffile1 file1
ifiled1 filed2
l
e
2

f
i
e
l
d
2

7counter 3
6count 3
paste file2 file1 (not happening)
jayachandran.g
Regular Advisor

Re: Spliting and merging files

Hi All

to be clear on what we are taking i'm opening a new case.

thanks to all