All of these solutions require processing (beyond simple reading) the entire line. It would be simpler to look for the first non-numeric, or, depending on the OP's needs, the more restrictive first alphabetic character.
Regards,
Alister
Why use awk, grep, or sed when it can all be done with ksh built-ins:
It would take a fairly large input file to make the invocation costs of starting up an external binary less expensive than the costs of running this simple loop in the shell.
Note that the problem statement is not clear about what should happen if an input line is a blank line. If blank lines are possible in the input, the specification needs to be extended to state what should happen in this case. The while loop above will print non-empty blank lines, but will not print empty lines.
PS Jotne:
Quote:
Why use [0-9][0-9]* (one digit + 0 or more digit), when you can use [0-9]+ (one or more digit)
What you suggested will work with:
which uses extended regular expressions, but not with:
which uses basic regular expressions.
This last grep command above will only print lines that contain a digit immediately followed by a plus sign.
Last edited by Don Cragun; 08-30-2013 at 04:00 PM..
Reason: Fix typo.
Why use awk, grep, or sed when it can all be done with ksh built-ins:
One reason would be human readability.
Quote:
Originally Posted by Don Cragun
The equivalent grep '[^0-9]' test.txt is instantly understandable, while that while-loop takes some time to digest.
Another reason is that the simpler the code the fewer the opportunities for surprises. Your while-loop, for example, would behave badly if there are backslashes in the data (the OP made no assurance against this possibility).
If I were going to do this with sh builtins, I would use case. To me it's more obvious.
Quote:
Originally Posted by Don Cragun
It would take a fairly large input file to make the invocation costs of starting up an external binary less expensive than the costs of running this simple loop in the shell.
Perhaps, but I would categorize that under premature optimization.
I have a directory of files, I can show the number of lines in each file and order them from lowest to highest with:
wc -l *|sort
15263 Image.txt
16401 reference.txt
40459 richtexteditor.txt
How can I also print the number of unique lines in each file?
15263 1401 Image.txt
16401... (15 Replies)
Hi All,
I am looking for a perl/awk/sed command to auto-increment the numbers line in file, P1.tcl:
run_build_model sparc_ifu_dec
run_drc
set_faults -model path_delay -atpg_effectiveness -fault_coverage
add_delay_paths P1
set_atpg -abort_limit 1000
run_atpg -ndetects 1000
I would like... (6 Replies)
Hi,
I have a sample file as shown below, I am looking for sed or any command which prints the complete word only from the input file.
Ex:
$ cat "sample.log"
I am searching for a word which is present in this file
We can do a pattern search using grep but I need to cut only the word which... (1 Reply)
Hi,
I am trying to search for a word and print the next word. For example:
My text is "<TRANSFORMATION TYPE ="Lookup Procedure">"
I am searching for "TYPE" and trying to print ="Lookup Procedure"
I have written a code like following:
echo $line | nawk... (4 Replies)
hi all, I'm new there, I'm just playing with perl and lwp and I just successfully created a script for log in to a web site with post. I have a response but I would like to have something like this:
I have in my response lines like:
<div class="sender">mimi020</div>
<some html code.....>... (3 Replies)
Hi
can we grep for a word and print the next word of the greped word?
ex:- create or replace function function_name
create function function_name
we should search for word "function" and output next word "function_name" from both lines. (3 Replies)
Hi, i am new to unix shell scripting and i need a script which would search for a particular word in all the files present in a directory. The output should have the word and file path name. For example: "word" "path name".
Thanks for the reply in adv,:) (3 Replies)
Hi All,
I wanted to know if there is a shell command to print a word n number of times
The Input File is :
Cat 4
Bat 3
Zall 1
Kite 2
Output File required is :
Cat
Cat
Cat
Cat
Bat
Bat
Bat
Zall
Kite (4 Replies)