Print only word not number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print only word not number
# 1  
Old 08-30-2013
Print only word not number

Hi,

Need to extract only words not numbers

Code:
#cat test.txt
123456
oracle
web
56789
s21adm

Required output

Code:
#grep <options> test.txt
oracle
web
s21adm

Note, in between integer "s21adm" is required but not with full integer "123456" and "56789"

Any help? I am using AIX Ksh.

Regards,
Siva
# 2  
Old 08-30-2013
Code:
awk '!/^[0-9]+$/' test.txt
oracle
web
s21adm

# 3  
Old 08-30-2013
try also:
Code:
sed -n '/^[0-9][0-9]*$/!p' test.txt
awk '!($0+0)' test.txt
grep -v "^[0-9][0-9]*$" test.txt

# 4  
Old 08-30-2013
Quote:
Originally Posted by Jotne
Code:
awk '!/^[0-9]+$/' test.txt
oracle
web
s21adm

Quote:
Originally Posted by rdrtx1
try also:
Code:
sed -n '/^[0-9][0-9]*$/!p' test.txt
awk '!($0+0)' test.txt
grep -v "^[0-9][0-9]*$" test.txt

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.
Code:
grep '[^[:digit:]]' file
grep '[[:alpha:]]' file

Regards,
Alister
# 5  
Old 08-30-2013
@Rdrtx1
Code:
awk '!($0+0)'

This fail if value of line is 0

Why use [0-9][0-9]* (one digit + 0 or more digit), when you can use [0-9]+ (one or more digit)
# 6  
Old 08-30-2013
Why use awk, grep, or sed when it can all be done with ksh built-ins:
Code:
while IFS="" read x
do      if [ "$x" != "${x%*[!0-9]*}" ]
        then    printf "%s\n" "$x"
        fi
done < test.txt

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:
Code:
grep -E '[0-9]+'

which uses extended regular expressions, but not with:
Code:
grep '[0-9]+'

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.
# 7  
Old 08-30-2013
Quote:
Originally Posted by Don Cragun
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
Code:
while IFS="" read x
do      if [ "$x" != "${x%*[!0-9]*}" ]
        then    printf "%s\n" "$x"
        fi
done < test.txt

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.
Code:
case $x in
*[!0-9]*) printf '%s\n' "$x"
esac

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.

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Print number of lines for files in directory, also print number of unique lines

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)
Discussion started by: spacegoose
15 Replies

2. Shell Programming and Scripting

Find a word and increment the number in the word & save into new files

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)
Discussion started by: jypark22
6 Replies

3. Shell Programming and Scripting

Search for a specific word and print only the word from the input file

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)
Discussion started by: mohan_kumarcs
1 Replies

4. Shell Programming and Scripting

[Solved] Search for a word and print the next word

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)
Discussion started by: sampoorna
4 Replies

5. Shell Programming and Scripting

Help with sort word followed by exponential number and numeric number at the same time

Input file: ID_34 2E-69 2324 ID_1 0E0 3254 ID_1 0E0 5434 ID_5 0E0 436 ID_1 1E-14 2524 ID_1 5E-52 46437 ID_3 65E-20 45467 ID_1 0E0 6578 ... Desired output file: ID_1 0E0 6578 ID_1 0E0 5434 ID_1 0E0 3254 ID_1 5E-52 46437 ID_1 1E-14 2524 ID_3 65E-20 45467 (5 Replies)
Discussion started by: cpp_beginner
5 Replies

6. Shell Programming and Scripting

perl lwp find word and print next word :)

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)
Discussion started by: vogueestylee
3 Replies

7. Shell Programming and Scripting

How ti Grep for a word and print the next word

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)
Discussion started by: manasa_vs
3 Replies

8. UNIX for Dummies Questions & Answers

Script to search for a particular word in files and print the word and path name

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)
Discussion started by: virtual_45
3 Replies

9. Shell Programming and Scripting

Print a word specific number of times

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)
Discussion started by: sam_2921
4 Replies

10. Shell Programming and Scripting

Counts a number of unique word contained in the file and print them in alphabetical order

What should be the Shell script that counts a number of unique word contained in a file and print them in alphabetical order line by line? (7 Replies)
Discussion started by: proactiveaditya
7 Replies
Login or Register to Ask a Question