Need assistance in ksh script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need assistance in ksh script
# 1  
Old 03-10-2015
Need assistance in ksh script

requirement : I need to read a text file and find out which particular line has highest charcters on it using the shell script.

I tried & was able to find out only for one line. I could not able to find out for the entire the line.

Code:
sed -n '10 p' ctstest.sh | wc -w

Please guide me out to how to read the entire file and find out the highest character present in the file.

Last edited by Scrutinizer; 03-10-2015 at 01:24 PM.. Reason: Removed spurious font format
# 2  
Old 03-10-2015
What do you define to be the "highest" char?
# 3  
Old 03-10-2015
Try
Code:
awk '{print NR, length}' file

which will print the number of characters per line (excluding newlines)

or
Code:
awk '{l=length} l>m{m=l; n=NR} END{print "line " n ": " m " characters"}' file

Which will print the line number with the highest number of characters (excluding newlines) ...
# 4  
Old 03-10-2015
Hello ramkumar15,

I have a few to questions pose in response first:-
  • Is this homework/assignment? There are specific forums for these.
  • What have you tried so far?
  • What output/errors do you get?
  • What OS and version are you using?
  • What are your preferred tools? (C, shell, perl, awk, etc.)
  • What logical process have you considered? (to help steer us to follow what you are trying to achieve)
Most importantly, What have you tried so far?

There are probably many ways to achieve most tasks, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.


We're all here to learn and getting the relevant information will help us all.

How about:-
Code:
#!/bin/ksh
i=0
while read line
do
   ((i=$i+1))
   printf "Line $i\t "${#line} characters\n"
done < file

It depends what you actually want. The thread title isn't exactly useful either.


Regards,
Robin
# 5  
Old 03-11-2015
hi rudic,

My requirement is to findout the highest number of characters used in the line in a particular text file and only the highest particual line need to be printed on it.

Hi rbatte,

Thanks for your inputs. I will make sure to try myself.

Hi scrutizer,
Thanks for your inputs.
# 6  
Old 03-11-2015
So, if you sort the output by the character count, you can get the line number. Do you want the content from that line? Using sed can extract that for you.


Let us know how you get on.



Regards,
Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Dummy script assistance

Hi, I am new in ksh scripting and if anyone can help that would be great. I'm writing a script which will SSH to several machines and then would append a certain file from a NAS to the /etc/sudoers file the problem i am having is after the script connects to a certain server the commands are... (7 Replies)
Discussion started by: galuzan
7 Replies

2. Shell Programming and Scripting

Need assistance with a simple script

I have a simple script. Do you know what I got this error? ./total_memory.ksh: line 5: ' Thanks #! /bin/bash setmem=30177660 totalMemory= grep MemTotal /proc/meminfo | awk '{print $2}' if ; then echo "Total memory $totalMemory is less than :$setmem" exit 1 ... (3 Replies)
Discussion started by: Beginer0705
3 Replies

3. Shell Programming and Scripting

Need assistance to resolve the KSH issue

am running the small script below. count_a=48 count_b=48 if ; then echo "Count matched" else echo "count not matched" fi I got the below output. /bin/ksh: [48: not found count not matched It was giving the same error when I ran in another box. But I inculded /bin/ksh in the... (10 Replies)
Discussion started by: sathik
10 Replies

4. Shell Programming and Scripting

script assistance with shift J

Hey all, I need some assistance. I'm writing a script to eject tapes from a tape library, but the library is not a queued system and can only eject 15 tapes at a time. I added paste -d : -s so that it goes through full_tapes and puts each media_id on one line separated by the :. Now I'm... (2 Replies)
Discussion started by: em23
2 Replies

5. Shell Programming and Scripting

Shell Script Assistance

I am looking for a shell script or command to automate a process of opening many files in a directory and changing a string of text. Example: I have a apache web server that uses virtual hosting. There are approximately 2300 vhost entries or files. So in the directory... (2 Replies)
Discussion started by: jaysunn
2 Replies

6. Shell Programming and Scripting

shell script assistance please

When I run this command (showstatus <username> <dbname>) in the prompt, the following will be displayed in the screen: 1. Show processes 2. Start process 3. Stop process 4. Go back to prompt Once i choose/type Option "1" (which is Show processes), it will display the list of processes... (5 Replies)
Discussion started by: xinoo
5 Replies

7. Shell Programming and Scripting

import var and function from ksh script to another ksh script

Ih all, i have multiples ksh scripts for crontab's unix jobs they all have same variables declarations and some similar functions i would have a only single script file to declare my variables, like: var1= "aaa" var2= "bbb" var3= "ccc" ... function ab { ...} function bc { ... }... (2 Replies)
Discussion started by: wolfhurt
2 Replies

8. Shell Programming and Scripting

need assistance ----SH schell script

Hello All, I need to develop a script(SH]) to generate a comparison file between two files old and new file.The script takes in parameter the old file path and the new file path. And the script generates a file containing the comparison between the two files with this details: - Keys... (2 Replies)
Discussion started by: shahidbakshi
2 Replies

9. Shell Programming and Scripting

Need a little assistance with a shell script

I need to modify a script to send an attatched file. I have researched and read the faq's but have not found a solution for my script. Here is a copy of the code I am using: #!/bin/sh mysqldump --opt --skip-add-locks --user=****** --password=******* databasename | gzip >... (3 Replies)
Discussion started by: rickou812
3 Replies

10. Shell Programming and Scripting

KSH Script Assistance

Hey everyone, I'm newer than new when it comes to this ksh and scripting stuff, and unix in general. I have been thrown into a task at work that A: They expect me to come up to speed on, B: Show I can do this. (Program for the workgroup) Here's the script, part of it, from the overall... (3 Replies)
Discussion started by: Brusimm
3 Replies
Login or Register to Ask a Question