niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » shell
joining lines in ksh
Post new topic   Reply to topic Page 1 of 1 [14 Posts] View previous topic :: View next topic
Author Message
Rakesh Sharma
*nix forums beginner


Joined: 28 Feb 2005
Posts: 36

PostPosted: Fri Jul 21, 2006 10:07 am    Post subject: Re: joining lines in ksh Reply with quote

jctown@nb.sympatico.ca wrote:
Quote:
I have a file that looks like this

category1
member1-1
member1-2
member1-3
member1-4
member1-5
member1-6
category2
member2-1
member2-2
member2-3
member2-4
member2-5
member2-6
category3
member3-1
member3-2

...............[snip]



sed -ne '/^category/h;//!G;s/\(.*\)\n\(.*\)/\2,\1/p' inputfile
Back to top
jctown@nb.sympatico.ca
*nix forums beginner


Joined: 19 Jul 2006
Posts: 6

PostPosted: Thu Jul 20, 2006 1:17 am    Post subject: Re: joining lines in ksh Reply with quote

Chris F.A. Johnson wrote:
Quote:
On 2006-07-19, jctown@nb.sympatico.ca wrote:
[snip]

Here's my version of your script... my posix complained about [ $? -eq
0 ] so I put it back to [[ ]]

What, exactly, was the complaint? [[...]] is not POSIX.

count=1
while read line junk ## $junk will contain everything after the first word
do
case $line in
*".jar"*) true ;;
*) false ;;
esac
if [[ $? -eq 0 ]]
then
save_category=$line
else
echo $count "," $save_category "," $line
let count=$count+1

As I said before, that is not POSIX (actually, I think I said it
was not portable -- same thing).

count=$(( $count + 1 ))

fi
done < xxx.txt > new_one.txt



--
Chris F.A. Johnson, author <http://cfaj.freeshell.org
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence

I put a she-bang at the top to force ksh....and it worked with [..] No
syntax errors
Back to top
Chris F.A. Johnson
*nix forums Guru


Joined: 20 Feb 2005
Posts: 2268

PostPosted: Thu Jul 20, 2006 12:45 am    Post subject: Re: joining lines in ksh Reply with quote

On 2006-07-19, jctown@nb.sympatico.ca wrote:
[snip]
Quote:

Here's my version of your script... my posix complained about [ $? -eq
0 ] so I put it back to [[ ]]

What, exactly, was the complaint? [[...]] is not POSIX.

Quote:
count=1
while read line junk ## $junk will contain everything after the first word
do
case $line in
*".jar"*) true ;;
*) false ;;
esac
if [[ $? -eq 0 ]]
then
save_category=$line
else
echo $count "," $save_category "," $line
let count=$count+1

As I said before, that is not POSIX (actually, I think I said it
was not portable -- same thing).

count=$(( $count + 1 ))

Quote:
fi
done < xxx.txt > new_one.txt



--
Chris F.A. Johnson, author <http://cfaj.freeshell.org>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Back to top
jctown@nb.sympatico.ca
*nix forums beginner


Joined: 19 Jul 2006
Posts: 6

PostPosted: Wed Jul 19, 2006 11:48 pm    Post subject: Re: joining lines in ksh Reply with quote

Chris F.A. Johnson wrote:
Quote:
On 2006-07-19, jctown@nb.sympatico.ca wrote:

Ed Morton wrote:
Jon LaBadie wrote:

jctown@nb.sympatico.ca wrote:

I have a file that looks like this

category1
member1-1
member1-2
member1-3
member1-4
member1-5
member1-6
category2
member2-1
member2-2
member2-3
member2-4
member2-5
member2-6
category3
member3-1
member3-2


I have 432 categories

What I need is a program that will read through the file and create a
new file based on
the following pseudo code

while not eof input
read a line
if it's a category
save the category
endif
read a line
if it's not a category
create a new line with saved category,member
end

Which would result in something line this

category1,member1-1
category1,member1-2
.
.
category2,member2-1
category2,member-2-
.
.
My pseudo-code is not 100% accurate, but I think it's close enough.


As a ksh solution was requested:


while read rec
do
case "$rec"
in
category*) CAT="$rec" ;;
*) echo "$CAT,$rec" ;;
esac
done

Not a huge deal, but the normal convention for naming shell variables is
that all-upper-case is only used for exported variables. In the above
case, you run into the slight problem that if you make your variable
lower case then it becomes "cat" which clashes with the command name and
so could create some confusion to someone reading the script in future
and cause bizarre behavior if you use your variable incorrectly in a
context where "cat" gets interpretted as the command name. For example
if you mistakenly wrote this:

cat ="$rec"

instead of this:

cat="$rec"

then you'd get an error message about "cat" not being able to open the
file "=category1,member1-1".

To get round that, another convention that some people (apparently only
me in this NG so I often don't follow my own advice when posting here!)
use is to start all non-exported variables with an underscore as well as
make them lower (actually mixed) case. You also don't NEED the quotes
around "$rec" in the case statement. So, the above could be written as:

while read rec
do
case $rec in
category*) _cat="$rec" ;;
*) echo "$_cat,$rec" ;;
esac
done

Regards,

Ed.

believe it or not, the solution was rather simple.

# count is a unique number so that I can use it as an index on a
database table
count=1
for line in $( cat text_file | awk '{print $1}' )

That is a useless use of cat, and an unnecessary use of awk. The
only point to using awk is to mitigate this incorrect method of
reading a file. Use:

while read line junk ## $junk will contain everything after the first word

do
echo $line | grep "a string"

There is no need for grep. Use case:

case $line in
*"a string"*) true ;;
*) false ;;
esac

if [[ $? -eq 0 ]]

Use [ $? -eq 0 ], as [[...]] is not portable.

then
# i need to save my category
save_category=$line
else
# build a line to put out
echo $count "," $save_category "," $line >> output_file

Your script will be faster if you put the redirection outside the
loop.

let count=$count+1

Use the portable form:

count=$(( $count + 1 ))

fi
done

done < text_file > output_file

It took a while to run, but I was able to create my file, 118000+ lines


--
Chris F.A. Johnson, author <http://cfaj.freeshell.org
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence

Hi,

Here's my version of your script... my posix complained about [ $? -eq
0 ] so I put it back to [[ ]]

count=1
while read line junk ## $junk will contain everything after the first
word
do
case $line in
*".jar"*) true ;;
*) false ;;
esac
if [[ $? -eq 0 ]]
then
save_category=$line
else
echo $count "," $save_category "," $line
let count=$count+1
fi
done < xxx.txt > new_one.txt
Back to top
jctown@nb.sympatico.ca
*nix forums beginner


Joined: 19 Jul 2006
Posts: 6

PostPosted: Wed Jul 19, 2006 11:29 pm    Post subject: Re: joining lines in ksh Reply with quote

Chris F.A. Johnson wrote:
Quote:
On 2006-07-19, jctown@nb.sympatico.ca wrote:

Ed Morton wrote:
Jon LaBadie wrote:

jctown@nb.sympatico.ca wrote:

I have a file that looks like this

category1
member1-1
member1-2
member1-3
member1-4
member1-5
member1-6
category2
member2-1
member2-2
member2-3
member2-4
member2-5
member2-6
category3
member3-1
member3-2


I have 432 categories

What I need is a program that will read through the file and create a
new file based on
the following pseudo code

while not eof input
read a line
if it's a category
save the category
endif
read a line
if it's not a category
create a new line with saved category,member
end

Which would result in something line this

category1,member1-1
category1,member1-2
.
.
category2,member2-1
category2,member-2-
.
.
My pseudo-code is not 100% accurate, but I think it's close enough.


As a ksh solution was requested:


while read rec
do
case "$rec"
in
category*) CAT="$rec" ;;
*) echo "$CAT,$rec" ;;
esac
done

Not a huge deal, but the normal convention for naming shell variables is
that all-upper-case is only used for exported variables. In the above
case, you run into the slight problem that if you make your variable
lower case then it becomes "cat" which clashes with the command name and
so could create some confusion to someone reading the script in future
and cause bizarre behavior if you use your variable incorrectly in a
context where "cat" gets interpretted as the command name. For example
if you mistakenly wrote this:

cat ="$rec"

instead of this:

cat="$rec"

then you'd get an error message about "cat" not being able to open the
file "=category1,member1-1".

To get round that, another convention that some people (apparently only
me in this NG so I often don't follow my own advice when posting here!)
use is to start all non-exported variables with an underscore as well as
make them lower (actually mixed) case. You also don't NEED the quotes
around "$rec" in the case statement. So, the above could be written as:

while read rec
do
case $rec in
category*) _cat="$rec" ;;
*) echo "$_cat,$rec" ;;
esac
done

Regards,

Ed.

believe it or not, the solution was rather simple.

# count is a unique number so that I can use it as an index on a
database table
count=1
for line in $( cat text_file | awk '{print $1}' )

That is a useless use of cat, and an unnecessary use of awk. The
only point to using awk is to mitigate this incorrect method of
reading a file. Use:

while read line junk ## $junk will contain everything after the first word

do
echo $line | grep "a string"

There is no need for grep. Use case:

case $line in
*"a string"*) true ;;
*) false ;;
esac

if [[ $? -eq 0 ]]

Use [ $? -eq 0 ], as [[...]] is not portable.

then
# i need to save my category
save_category=$line
else
# build a line to put out
echo $count "," $save_category "," $line >> output_file

Your script will be faster if you put the redirection outside the
loop.

let count=$count+1

Use the portable form:

count=$(( $count + 1 ))

fi
done

done < text_file > output_file

It took a while to run, but I was able to create my file, 118000+ lines


--
Chris F.A. Johnson, author <http://cfaj.freeshell.org
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence


Thank you so much for the advice...my script isn't elegant but it
works... it was a one-shot deal and not for public consumption...
However I will take what you have shown and give it a try as well.

Cheers
Back to top
Chris F.A. Johnson
*nix forums Guru


Joined: 20 Feb 2005
Posts: 2268

PostPosted: Wed Jul 19, 2006 8:45 pm    Post subject: Re: joining lines in ksh Reply with quote

On 2006-07-19, jctown@nb.sympatico.ca wrote:
Quote:

Ed Morton wrote:
Jon LaBadie wrote:

jctown@nb.sympatico.ca wrote:

I have a file that looks like this

category1
member1-1
member1-2
member1-3
member1-4
member1-5
member1-6
category2
member2-1
member2-2
member2-3
member2-4
member2-5
member2-6
category3
member3-1
member3-2


I have 432 categories

What I need is a program that will read through the file and create a
new file based on
the following pseudo code

while not eof input
read a line
if it's a category
save the category
endif
read a line
if it's not a category
create a new line with saved category,member
end

Which would result in something line this

category1,member1-1
category1,member1-2
.
.
category2,member2-1
category2,member-2-
.
.
My pseudo-code is not 100% accurate, but I think it's close enough.


As a ksh solution was requested:


while read rec
do
case "$rec"
in
category*) CAT="$rec" ;;
*) echo "$CAT,$rec" ;;
esac
done

Not a huge deal, but the normal convention for naming shell variables is
that all-upper-case is only used for exported variables. In the above
case, you run into the slight problem that if you make your variable
lower case then it becomes "cat" which clashes with the command name and
so could create some confusion to someone reading the script in future
and cause bizarre behavior if you use your variable incorrectly in a
context where "cat" gets interpretted as the command name. For example
if you mistakenly wrote this:

cat ="$rec"

instead of this:

cat="$rec"

then you'd get an error message about "cat" not being able to open the
file "=category1,member1-1".

To get round that, another convention that some people (apparently only
me in this NG so I often don't follow my own advice when posting here!)
use is to start all non-exported variables with an underscore as well as
make them lower (actually mixed) case. You also don't NEED the quotes
around "$rec" in the case statement. So, the above could be written as:

while read rec
do
case $rec in
category*) _cat="$rec" ;;
*) echo "$_cat,$rec" ;;
esac
done

Regards,

Ed.

believe it or not, the solution was rather simple.

# count is a unique number so that I can use it as an index on a
database table
count=1
for line in $( cat text_file | awk '{print $1}' )

That is a useless use of cat, and an unnecessary use of awk. The
only point to using awk is to mitigate this incorrect method of
reading a file. Use:

while read line junk ## $junk will contain everything after the first word

Quote:
do
echo $line | grep "a string"

There is no need for grep. Use case:

case $line in
*"a string"*) true ;;
*) false ;;
esac

Quote:
if [[ $? -eq 0 ]]

Use [ $? -eq 0 ], as [[...]] is not portable.

Quote:
then
# i need to save my category
save_category=$line
else
# build a line to put out
echo $count "," $save_category "," $line >> output_file

Your script will be faster if you put the redirection outside the
loop.

Quote:
let count=$count+1

Use the portable form:

count=$(( $count + 1 ))

Quote:
fi
done

done < text_file > output_file

Quote:
It took a while to run, but I was able to create my file, 118000+ lines


--
Chris F.A. Johnson, author <http://cfaj.freeshell.org>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Back to top
jctown@nb.sympatico.ca
*nix forums beginner


Joined: 19 Jul 2006
Posts: 6

PostPosted: Wed Jul 19, 2006 8:08 pm    Post subject: Re: joining lines in ksh Reply with quote

Jon LaBadie wrote:
Quote:
jctown@nb.sympatico.ca wrote:
Ed Morton wrote:
Jon LaBadie wrote:

jctown@nb.sympatico.ca wrote:

I have a file that looks like this

category1
member1-1
member1-2
member1-3
member1-4
member1-5
member1-6
category2
member2-1
...
What I need is a program that will read through the file and create a
new file based on
...
Which would result in something line this

category1,member1-1
category1,member1-2
.
.
category2,member2-1
category2,member-2-
.
.
...
while read rec
do
case $rec in
category*) _cat="$rec" ;;
*) echo "$_cat,$rec" ;;
esac
done

Regards,

Ed.

believe it or not, the solution was rather simple.

# count is a unique number so that I can use it as an index on a
database table
count=1
for line in $( cat text_file | awk '{print $1}' )
do
echo $line | grep "a string"
if [[ $? -eq 0 ]]
then
# i need to save my category
save_category=$line
else
# build a line to put out
echo $count "," $save_category "," $line >> output_file
let count=$count+1
fi
done

It took a while to run, but I was able to create my file, 118000+ lines


Glad you found a solution.

Yes it would take a while to run, you did 118000 creations of a pipe,
118000 start-up and runs of grep, 118000 openings of output_file.

Your original requirement did not show any need to extract column
one from the input, in fact the sample data only had one column.
Nor did you mention the need to add a counter.

Should you have to repeat the run, I think you would find considerable
speed-up by doing those actions once, or in the shell rather than a
separate program.

Using Ed's code as a base:

count=0
while read rec
do
case $rec in
*a_string*) _cat="$rec" ;;
*) let count=$count+1
echo "$count,$_cat,$rec"
;;
esac
done < text_file > output_file

And if you need the first column extraction, a small change:

awk '{print $1}' text_file |
while read rec
...
done > output_file

There is only one column in the input file, I just want to make sure I
didn't get any other stuff on the line that might confuse the issue.
Back to top
Jon LaBadie
*nix forums beginner


Joined: 06 May 2006
Posts: 46

PostPosted: Wed Jul 19, 2006 4:56 pm    Post subject: Re: joining lines in ksh Reply with quote

jctown@nb.sympatico.ca wrote:
Quote:
Ed Morton wrote:
Jon LaBadie wrote:

jctown@nb.sympatico.ca wrote:

I have a file that looks like this

category1
member1-1
member1-2
member1-3
member1-4
member1-5
member1-6
category2
member2-1
....
What I need is a program that will read through the file and create a
new file based on
....
Which would result in something line this

category1,member1-1
category1,member1-2
.
.
category2,member2-1
category2,member-2-
.
.
....
while read rec
do
case $rec in
category*) _cat="$rec" ;;
*) echo "$_cat,$rec" ;;
esac
done

Regards,

Ed.

believe it or not, the solution was rather simple.

# count is a unique number so that I can use it as an index on a
database table
count=1
for line in $( cat text_file | awk '{print $1}' )
do
echo $line | grep "a string"
if [[ $? -eq 0 ]]
then
# i need to save my category
save_category=$line
else
# build a line to put out
echo $count "," $save_category "," $line >> output_file
let count=$count+1
fi
done

It took a while to run, but I was able to create my file, 118000+ lines


Glad you found a solution.

Yes it would take a while to run, you did 118000 creations of a pipe,
118000 start-up and runs of grep, 118000 openings of output_file.

Your original requirement did not show any need to extract column
one from the input, in fact the sample data only had one column.
Nor did you mention the need to add a counter.

Should you have to repeat the run, I think you would find considerable
speed-up by doing those actions once, or in the shell rather than a
separate program.

Using Ed's code as a base:

count=0
while read rec
do
case $rec in
*a_string*) _cat="$rec" ;;
*) let count=$count+1
echo "$count,$_cat,$rec"
;;
esac
done < text_file > output_file

And if you need the first column extraction, a small change:

awk '{print $1}' text_file |
while read rec
...
done > output_file
Back to top
jctown@nb.sympatico.ca
*nix forums beginner


Joined: 19 Jul 2006
Posts: 6

PostPosted: Wed Jul 19, 2006 3:49 pm    Post subject: Re: joining lines in ksh Reply with quote

Ed Morton wrote:
Quote:
Jon LaBadie wrote:

jctown@nb.sympatico.ca wrote:

I have a file that looks like this

category1
member1-1
member1-2
member1-3
member1-4
member1-5
member1-6
category2
member2-1
member2-2
member2-3
member2-4
member2-5
member2-6
category3
member3-1
member3-2


I have 432 categories

What I need is a program that will read through the file and create a
new file based on
the following pseudo code

while not eof input
read a line
if it's a category
save the category
endif
read a line
if it's not a category
create a new line with saved category,member
end

Which would result in something line this

category1,member1-1
category1,member1-2
.
.
category2,member2-1
category2,member-2-
.
.
My pseudo-code is not 100% accurate, but I think it's close enough.


As a ksh solution was requested:


while read rec
do
case "$rec"
in
category*) CAT="$rec" ;;
*) echo "$CAT,$rec" ;;
esac
done

Not a huge deal, but the normal convention for naming shell variables is
that all-upper-case is only used for exported variables. In the above
case, you run into the slight problem that if you make your variable
lower case then it becomes "cat" which clashes with the command name and
so could create some confusion to someone reading the script in future
and cause bizarre behavior if you use your variable incorrectly in a
context where "cat" gets interpretted as the command name. For example
if you mistakenly wrote this:

cat ="$rec"

instead of this:

cat="$rec"

then you'd get an error message about "cat" not being able to open the
file "=category1,member1-1".

To get round that, another convention that some people (apparently only
me in this NG so I often don't follow my own advice when posting here!)
use is to start all non-exported variables with an underscore as well as
make them lower (actually mixed) case. You also don't NEED the quotes
around "$rec" in the case statement. So, the above could be written as:

while read rec
do
case $rec in
category*) _cat="$rec" ;;
*) echo "$_cat,$rec" ;;
esac
done

Regards,

Ed.

believe it or not, the solution was rather simple.

# count is a unique number so that I can use it as an index on a
database table
count=1
for line in $( cat text_file | awk '{print $1}' )
do
echo $line | grep "a string"
if [[ $? -eq 0 ]]
then
# i need to save my category
save_category=$line
else
# build a line to put out
echo $count "," $save_category "," $line >> output_file
let count=$count+1
fi
done

It took a while to run, but I was able to create my file, 118000+ lines
Back to top
Ed Morton
*nix forums Guru


Joined: 20 Feb 2005
Posts: 1073

PostPosted: Wed Jul 19, 2006 1:47 pm    Post subject: Re: joining lines in ksh Reply with quote

Jon LaBadie wrote:

Quote:
jctown@nb.sympatico.ca wrote:

I have a file that looks like this

category1
member1-1
member1-2
member1-3
member1-4
member1-5
member1-6
category2
member2-1
member2-2
member2-3
member2-4
member2-5
member2-6
category3
member3-1
member3-2


I have 432 categories

What I need is a program that will read through the file and create a
new file based on
the following pseudo code

while not eof input
read a line
if it's a category
save the category
endif
read a line
if it's not a category
create a new line with saved category,member
end

Which would result in something line this

category1,member1-1
category1,member1-2
.
.
category2,member2-1
category2,member-2-
.
.
My pseudo-code is not 100% accurate, but I think it's close enough.


As a ksh solution was requested:


while read rec
do
case "$rec"
in
category*) CAT="$rec" ;;
*) echo "$CAT,$rec" ;;
esac
done

Not a huge deal, but the normal convention for naming shell variables is
that all-upper-case is only used for exported variables. In the above
case, you run into the slight problem that if you make your variable
lower case then it becomes "cat" which clashes with the command name and
so could create some confusion to someone reading the script in future
and cause bizarre behavior if you use your variable incorrectly in a
context where "cat" gets interpretted as the command name. For example
if you mistakenly wrote this:

cat ="$rec"

instead of this:

cat="$rec"

then you'd get an error message about "cat" not being able to open the
file "=category1,member1-1".

To get round that, another convention that some people (apparently only
me in this NG so I often don't follow my own advice when posting here!)
use is to start all non-exported variables with an underscore as well as
make them lower (actually mixed) case. You also don't NEED the quotes
around "$rec" in the case statement. So, the above could be written as:

while read rec
do
case $rec in
category*) _cat="$rec" ;;
*) echo "$_cat,$rec" ;;
esac
done

Regards,

Ed.
Back to top
Jon LaBadie
*nix forums beginner


Joined: 06 May 2006
Posts: 46

PostPosted: Wed Jul 19, 2006 5:16 am    Post subject: Re: joining lines in ksh Reply with quote

jctown@nb.sympatico.ca wrote:
Quote:
I have a file that looks like this

category1
member1-1
member1-2
member1-3
member1-4
member1-5
member1-6
category2
member2-1
member2-2
member2-3
member2-4
member2-5
member2-6
category3
member3-1
member3-2


I have 432 categories

What I need is a program that will read through the file and create a
new file based on
the following pseudo code

while not eof input
read a line
if it's a category
save the category
endif
read a line
if it's not a category
create a new line with saved category,member
end

Which would result in something line this

category1,member1-1
category1,member1-2
.
.
category2,member2-1
category2,member-2-
.
.
My pseudo-code is not 100% accurate, but I think it's close enough.


As a ksh solution was requested:


while read rec
do
case "$rec"
in
category*) CAT="$rec" ;;
*) echo "$CAT,$rec" ;;
esac
done
Back to top
John W. Krahn
*nix forums Guru


Joined: 27 Feb 2005
Posts: 602

PostPosted: Wed Jul 19, 2006 4:30 am    Post subject: Re: joining lines in ksh Reply with quote

jctown@nb.sympatico.ca wrote:
Quote:
I have a file that looks like this

category1
member1-1
member1-2
member1-3
member1-4
member1-5
member1-6
category2
member2-1
member2-2
member2-3
member2-4
member2-5
member2-6
category3
member3-1
member3-2


I have 432 categories

[snip]

Which would result in something line this

category1,member1-1
category1,member1-2
.
.
category2,member2-1
category2,member-2-

$ echo "category1
member1-1
member1-2
member1-3
member1-4
member1-5
member1-6
category2
member2-1
member2-2
member2-3
member2-4
member2-5
member2-6
category3
member3-1
member3-2" | perl -lne'/^category/?($cat=$_):print"$cat,$_"'
category1,member1-1
category1,member1-2
category1,member1-3
category1,member1-4
category1,member1-5
category1,member1-6
category2,member2-1
category2,member2-2
category2,member2-3
category2,member2-4
category2,member2-5
category2,member2-6
category3,member3-1
category3,member3-2



John
--
use Perl;
program
fulfillment
Back to top
Chris F.A. Johnson
*nix forums Guru


Joined: 20 Feb 2005
Posts: 2268

PostPosted: Wed Jul 19, 2006 1:55 am    Post subject: Re: joining lines in ksh Reply with quote

On 2006-07-19, jctown@nb.sympatico.ca wrote:
Quote:
I have a file that looks like this

category1
member1-1
member1-2
member1-3
member1-4
member1-5
member1-6
category2
member2-1
member2-2
member2-3
member2-4
member2-5
member2-6
category3
member3-1
member3-2


I have 432 categories

What I need is a program that will read through the file and create a
new file based on
the following pseudo code

while not eof input
read a line
if it's a category
save the category
endif
read a line
if it's not a category
create a new line with saved category,member
end

Which would result in something line this

category1,member1-1
category1,member1-2
.
.
category2,member2-1
category2,member-2-
.
.

awk '/^category/ {category = $0; next}
{ print category "," $0 }' FILENAME > NEWFILE

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Back to top
jctown@nb.sympatico.ca
*nix forums beginner


Joined: 19 Jul 2006
Posts: 6

PostPosted: Wed Jul 19, 2006 1:28 am    Post subject: joining lines in ksh Reply with quote

I have a file that looks like this

category1
member1-1
member1-2
member1-3
member1-4
member1-5
member1-6
category2
member2-1
member2-2
member2-3
member2-4
member2-5
member2-6
category3
member3-1
member3-2


I have 432 categories

What I need is a program that will read through the file and create a
new file based on
the following pseudo code

while not eof input
read a line
if it's a category
save the category
endif
read a line
if it's not a category
create a new line with saved category,member
end

Which would result in something line this

category1,member1-1
category1,member1-2
..
..
category2,member2-1
category2,member-2-
..
..
My pseudo-code is not 100% accurate, but I think it's close enough.
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [14 Posts] View previous topic :: View next topic
The time now is Fri Nov 21, 2008 7:08 pm | All times are GMT
navigation Forum index » Programming » shell
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts blocking mails with long lines and giberish Dale Blount Postfix 4 Thu Jul 20, 2006 2:31 pm
No new posts Match lines, group and sum FriesAOLE shell 2 Thu Jul 20, 2006 7:40 am
No new posts Reading long lines doesn't work in Python Scott Simpson python 2 Wed Jul 19, 2006 4:25 pm
No new posts combine lines bp Perl 1 Tue Jul 18, 2006 11:48 pm
No new posts Getting all lines with last 14 hours contracer11@gmail.com shell 6 Tue Jul 18, 2006 3:50 pm

Mobile Phones | Credit Cards | Outsourcing | Loans | Credit Card
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.5916s ][ Queries: 20 (0.3848s) ][ GZIP on - Debug on ]