1834493 Members
2873 Online
110067 Solutions
New Discussion

Korn shell programming

 
SOLVED
Go to solution
Jasmin Berube
Advisor

Korn shell programming

I got a question regarding Korn shell programming and i'm wondering in which section i might post it?
14 REPLIES 14
James R. Ferguson
Acclaimed Contributor

Re: Korn shell programming

Jasmin:

Here is fine. What is your question?

...JRF...
Patrick Wallek
Honored Contributor

Re: Korn shell programming

Go ahead and post it here. We'll help you as best we can.
Jasmin Berube
Advisor

Re: Korn shell programming

ok here is my question?
i got a file sorted by user name (at the beginning of the line) and i want to split it out or generation other files for each user, fo exemple if my file have 3 lines beginning with Jasmin, 12 lines beginning with Bob and 7 lines with Fred, how i can take those lines out of this file to create new files for each users? I got about 200 users so i want to make a script to do it!
James R. Ferguson
Acclaimed Contributor

Re: Korn shell programming

Jasmin:

'grep' is one way:

# grep -i Jasmin /tmp/input > /tmp/output

The above looks case-insensitively for "Jasmin" in /tmp/input, and writes matching entries to /tmp/output.

...JRF...
Steven Sim Kok Leong
Honored Contributor

Re: Korn shell programming

Hi,

Since the matching characters of Jasmin, Bob etc start at the beginning of the lines, if you are looking for precision and omit "Bob" occurring in the middle of the line, you may want to try:

grep "^Bob" LOG > Bob.log

Hope this helps. Regards.

Steven Sim
Brainbench MVP for Unix Admin
http://www.brainbench.com
Curtis Larson
Trusted Contributor

Re: Korn shell programming

cat yourfile | awk '{
name = $1;
print $0 > name;
}'
Patrick Wallek
Honored Contributor
Solution

Re: Korn shell programming

Here is a script that should do what you want to do:

cat users_file | sort -u >> users_file1
for i in `cat users_file1`
do
grep $i users_file >> $i.file
done

The first line of the script will do a cat of your data file, sort it, and only give you one entry for each user in user_file1. Then in the next line it takes each users_file1 entry and greps for it in users_file and writes the output to user_name.file. It will do this for every entry in users_file1. Change the file names in the script to reflect whatever the file names you want to use are.

I hope this helps you.
Maureen Gunkel
Trusted Contributor

Re: Korn shell programming

Jasmin,
Curtis' answer looks good, except you may want to replace the > with >>, so that you get all the lines. > means overwrite, and >> means append.
Mo
No matter where you go, there you are.
Jasmin Berube
Advisor

Re: Korn shell programming

Is there a unix command that reads each line and when current line is the same as the previous keeps this line until it hits a line different?
I've already heard that there a command to do that... i might be wrong.
Patrick Wallek
Honored Contributor

Re: Korn shell programming

If you are sorting something then you can use 'sort -u' and that will sort the output, but only give one of each item if there are multiples.
Ovidiu D. Raita
Valued Contributor

Re: Korn shell programming

Here's one more:

for i in `sort -u the_file | awk '{print $1}'`; do
grep "^$i" the_file >> $i.out
done

Ovidiu
Simple solutions to complex problems
Curtis Larson
Trusted Contributor

Re: Korn shell programming

you look at the sort command the -u option and the uniq command and see if they are what your looking for
James R. Ferguson
Acclaimed Contributor

Re: Korn shell programming

Jasmin:

With regard to your last question about reading and comparing to the previously read line; 'awk' scripts can do this.

Consider this example:

echo "1\n2\n3\n3\n3\n4\n4" | awk 'BEGIN {getline;X=$0;print $0};
{if ($0 !~ X) {print $0;X=$0}}'

Instead of printing seven lines 1,2,3,3,3,4,4
this would merely print four, unique lines: 1,2,3,4.

As noted in the preceeding posts, the '-u' option of sort or the 'uniq' filter are general methods for accomplishing this.

...JRF...
Steven Sim Kok Leong
Honored Contributor

Re: Korn shell programming

Hi,

To reduce the number of iterations within the for loop for performance reasons, add an additional "| uniq" behind "awk '{print $1}'" in Ovidiu's script.

Hope this helps. Regards.

Steven Sim
Brainbench MVP for Unix Admin
http://www.brainbench.com