Bash script find longest line/lines in several files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script find longest line/lines in several files
# 8  
Old 10-26-2010
Well, i am bit confused... So i make changes you was writing... Script isn't working, so you are right, i need to check it... Thanks a lot... And that array.
I need to put the the longest line or lines into the array... Well, i thought that it could be like this
Code:
 
riadok="$(awk 'length==L{p=p RS L FS "\""$0"\""} length > L{L=length; p=L FS "\""$0"\""}END{ print p}' $0)"   ;    pole=("${pole[@]}" "$riadok")

And if this is not working, how can i put that lines into array?
# 9  
Old 10-26-2010
I a, having a hard time trying to figure out what you are trying to do. You mention using reading standard input, but I see no read statements. Besides that would not even be possible, at least you could only determine the longest line in standard input, not in multiple files.

Why would this not work:
Code:
print_help ()
{
    echo "The longest line in file or files"
    echo "-h shows help"
}

for i in  "$@"; do
  case "$1" in
     -h)  print_help
          exit 0
  esac
done

awk 'length==L{p=p RS L FS "\""$0"\""} length > L{L=length; p=L FS "\""$0"\""}END{ print p}' "$@"

This User Gave Thanks to Scrutinizer For This Post:
# 10  
Old 10-26-2010
It looks like i didn't tell clearly... Sorry for that... The input could be files, or nothing and in that case i should use stdin. Then should script find the longest possible line or eventually lines. Of course, -h could be there to echo help. I rewrite some of that rubbish that there was before... Now, my scritp looks like this....
Code:
#! /usr/local/bin/bash2

print_help ()
{
    echo "the longest line or lines in stdin or file or files"
    echo "-h show help"
}
riadok=""

for i in  "$@"; do
        case "$i" in
        -h)     vypis_helpu
                exit 0
                ;;
        *)
                riadok=$(awk 'length==L{r=r RS L FS ""$0""} length > L{L=length; r=L FS ""$0""}END{ print r}' "$i")
                pole=("${pole[@]}" "$riadok")
                ;;
        esac
        shift
done

for i in "${pole[@]}"; do
        echo "$i" | sort -n | tail -1
done

exit 0

Now i have a problem with set the output from awk to the variable riadok and then to put riadok into array. Hope, that this is more accurate than my last comment.



---------- Post updated at 10:16 AM ---------- Previous update was at 10:15 AM ----------




So it is "the longest line or lines in stdin or file or files"

Last edited by 1tempus1; 10-26-2010 at 07:34 AM..
# 11  
Old 10-26-2010
If you look at my suggestion, I believe it does everything you require. You can use it like this, using command line parameters
Code:
./script infile*

or like this, using stdin
Code:
cat infile* | ./script

# 12  
Old 10-26-2010
i tried to use awk inside of for to ensure, that awk will be executed for every file, that comes in. And then, the longest line or possible lines should be written to the variable. So in the and, i should have array of strings with the longest lines from all files... And from that array i choose the longest..



---------- Post updated at 10:27 AM ---------- Previous update was at 10:24 AM ----------




it gonna be automatic test and i shouldn't use cat... And i must figure out how to patch that for stdin inside script...
# 13  
Old 10-26-2010
The awk already does all that it chooses the longest line(s) from all files. I just used cat as an example to generate input on stdin for the shell script, you can use any process there. Inside the script the awk statement will then either read the files provided as parameters to the script or read from stdin if not file names are provided.

Last edited by Scrutinizer; 10-26-2010 at 07:52 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 14  
Old 10-26-2010
Well, i just finish testing... Awk is now good but it put all the lines into variable... So if i have 3 longest lines with same length, awk save all three to riadok... So it is not very good. But if awk find all longest lines from every file on input, how can i find the longest from the longest?



---------- Post updated at 11:14 AM ---------- Previous update was at 11:01 AM ----------




That code of yours isn't working, i tried to execute script ./script aaa bbb ccc...
and it didn't read stdin, just error message showed...



---------- Post updated at 11:34 AM ---------- Previous update was at 11:14 AM ----------




Code:
awk 'length==L{r=r RS L FS ""$0""} length > L{L=length; r=L FS ""$0""}END{ print r}' "$i"

I need to get output from this to the variable riadok. It works, when i've got files with only one the longest line in each file. But when it is more than that in single file, it put all lines into single variable. How can i fix this? I tried these options:
Code:
awk 'length==L{r=r RS L FS ""$0""} length > L{L=length; r=L FS ""$0""}END{ print r; riadok=r}' test001

Code:
awk 'length==L{r=r RS L FS ""$0""} length > L{L=length; r=L FS ""$0""}END{ print r; riadok=r; pole=("${pole[@]}" "$riadok")}' test001

But it doesn't work at all. Everytime it put all lines into single variable.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script using awk to find and replace a line, how to ignore comment lines

Hello, I have some code that works more or less. This is called by a make file to adjust some hard-coded definitions in the src code. The script generated some values by looking at some of the src files and then writes those values to specific locations in other files. The awk code is used to... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

2. Shell Programming and Scripting

Bash: copying lines with specific character to files with same name as copied line.

I am trying to make my script as simple as a possible but, I am not sure if the way I am approaching is necessarily the most efficient or effective it can be. What I am mainly trying to fix is a for loop to remove a string from the specified files and within this loop I am trying to copy the lines... (2 Replies)
Discussion started by: Allie_gastrator
2 Replies

3. UNIX for Advanced & Expert Users

How to find a string in a line in UNIX file and delete that line and previous 3 lines ?

Hi , i have a file with data as below.This is same file. But actual file contains to many rows. i want to search for a string "Field 039 00" and delete that line and previous 3 lines in that file.. Can some body suggested me how can i do using either sed or awk command ? Field 004... (7 Replies)
Discussion started by: vadlamudy
7 Replies

4. Shell Programming and Scripting

Shell Script to find common lines and replace next line

I want to find common line in two files and replace the next line of first file with the next line of second file. (sed,awk,perl,bash any solution is welcomed ) Case Ignored. Multiple Occurrence of same line. File 1: hgacdavd sndm,ACNMSDC msgid "Rome" msgstr "" kgcksdcgfkdsb... (4 Replies)
Discussion started by: madira
4 Replies

5. Shell Programming and Scripting

wc -L giving incorrect length of longest line

Running below line gives 3957 as length of longest line in file 20121119_SRMNotes_init.dat awk ' { if ( length > 3950 ) { x = length } }END{ print x }' 20121119_SRMNotes_init.dat While wc -L 20121119_SRMNotes_init.dat gives output as 4329. Why is there a difference between these two commands.... (2 Replies)
Discussion started by: Satish Mantha
2 Replies

6. Shell Programming and Scripting

Bash script to find the number of files and identify which ones are 0 bytes.

I am writing a bash script to find out all the files in a directory which are empty. I am running into multiple issues. I will really appreciate if someone can please help me. #!/bin/bash DATE=$(date +%m%d%y) TIME=$(date +%H%M) DIR="/home/statsetl/input/civil/test" ... (1 Reply)
Discussion started by: monasharma13
1 Replies

7. Shell Programming and Scripting

Find longest string and print it

Hello all, I need to find the longest string in a select field and print that field. I have tried a few different methods and I always end up one step from where I need to be. Methods thus far: nawk '{if (length($1) > long) long=length($1); if(length($1)==long) print $1}' The above... (6 Replies)
Discussion started by: SEinT
6 Replies

8. Shell Programming and Scripting

Finding longest line in a Record

Good Morning/Afternoon All, I am using the nawk utility in korn shell to find the longest field and display that result. My Data is as follows: The cat ran The elephant ran Milly ran too We all ran I have tried nawk '{ if (length($1) > len) len=length($1); print $1}' filename The... (5 Replies)
Discussion started by: SEinT
5 Replies

9. Shell Programming and Scripting

Shell script to find longest phrase

Hi Everyone, I am trying to write a shell script that can find the longest phrase that appears at least twice in an online news article. The HTML has been parsed through an HTML parser, converted to XML and the article content extracted. I have put this article content in a text file to work... (24 Replies)
Discussion started by: stargazerr
24 Replies

10. Shell Programming and Scripting

Find the length of the longest line

Dear All, To find the length of the longest line from a file i have used wc -L which is giving the proper output... But the problem is AIX os does not support wc -L command. so is there any other way 2 to find out the length of the longest line using awk or sed ? Regards, Pankaj (1 Reply)
Discussion started by: panknil
1 Replies
Login or Register to Ask a Question