Grep on a list


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Grep on a list
# 1  
Old 01-18-2015
Grep on a list

Hi,

If I have a list called list.txt and on each line theres a string of the form something*blah, how do I remove everything after the * for each item? Thanks!
# 2  
Old 01-18-2015
Hello jyu429,

Welcome to forums, kindly use code tags for commands/codes/Inputs used by you in the posts as per forum rules. Now for your requirement please do let us know the complete input and expected output for same, it will help us to advise you, based on some assumptions following you can take as a start up.

Lets say we have following input file.
Code:
cat test1
ABSCDET something*bla sdheufhkrm349875  eruhg38t jqd7842r
AJBUWEBDEK something*bla r3jgf743yt4in 34uf3984 u4hf9843y

Then following is the code for same, which may help.
Code:
awk '{gsub(/\*.*/,"*",$0);print}'  test1

Output will be as follows.
Code:
ABSCDET something*
AJBUWEBDEK something*

Thanks,
R. Singh
# 3  
Old 01-18-2015
Or sed:
Code:
sed 's/*.*/*/' file

To also remove the asterisk:
Code:
sed 's/*.*//' file

or
Code:
cut -d\* -f1 file


Last edited by Scrutinizer; 01-18-2015 at 10:41 AM..
# 4  
Old 01-18-2015
A literal * should be quoted in an RE
Code:
sed 's/\*.*//'

# 5  
Old 01-18-2015
Quote:
Originally Posted by MadeInGermany
A literal * should be quoted in an RE
Code:
sed 's/\*.*//'

Not if it is the first character...

Code:
*
The <asterisk> shall be special except when used:
In a bracket expression

As the first character of an entire BRE (after an initial '^', if any)

As the first character of a subexpression (after an initial '^', if any);

9.3.3 BRE Special Characters
# 6  
Old 01-18-2015
There are many old sed out there...
An escaped * is good style. And safe.
# 7  
Old 01-18-2015
It is part of to the POSIX specification. And so this will work fine with those older and compliant seds (and even seds that are older than that) .... So the assertion in #3 is inaccurate and I do not share this "good style and safe" opinion. It is immediately clear that the first asterisk cannot be anything other than literal, unless one assumes a mistake, which is not the case..

Last edited by Scrutinizer; 01-19-2015 at 12:17 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep to fetch exact list

Hi I have below lists of files filename-1.0.0.tar.gz filename-1.0.1.345657676.snapshots.tar.gz so when I do grep -o 'filename-*.tar.gz' | sort | tail -1 then it consider snapshots as a part of it's result. How can I make sure, that it should only display the results from... (3 Replies)
Discussion started by: manas_ranjan
3 Replies

2. Shell Programming and Scripting

Grep and ignore list from file

cat /tmp/i.txt '(ORA-28001|ORA-00100|ORA-28001|ORA-20026|ORA-20025|ORA-02291|ORA-01458|ORA-01017|ORA-1017|ORA-28000|ORA-06512|ORA-06512|Domestic Phone|ENCRYPTION)' grep -ia 'ORA-\{5\}:' Rep* |grep -iavE `cat /tmp/i.txt` grep: Unmatched ( or \( Please tell me why am i getting that (6 Replies)
Discussion started by: jhonnyrip
6 Replies

3. Shell Programming and Scripting

grep a variable from list

I have a script to create a variable from a list, B.txt and then search it in another file, file.txt and then print the pattern line and next line. #!/bin/bash while read a do echo "$a" | grep -A 1 $a file.txt > $a\.txt done < B.txt I always get that no such file or directory exists... (6 Replies)
Discussion started by: godzilla07
6 Replies

4. UNIX for Dummies Questions & Answers

grep list of directories

Hi all, it possible creating a file with list of directories, and using grep to find pattern on only directories and subdirectories contained in that file? And it is done? regards (4 Replies)
Discussion started by: bspirit
4 Replies

5. UNIX for Dummies Questions & Answers

List and grep

Hi, My requirement is to copy all yesterdays files which have "access" keyword in their filenames to a separate folder say "/tmp/moht". Can you please let me know how? (8 Replies)
Discussion started by: shifahim
8 Replies

6. Shell Programming and Scripting

Grep a files list

Hi, I want to grep a string from the list of files present in a text file. I want the output to be printed only when the string is present in a file along with the file name.. I tried some thing like cat files_list | grep "search_string" $_ I know $_ is wrong but i want something... (7 Replies)
Discussion started by: nigas
7 Replies

7. UNIX for Dummies Questions & Answers

How to Grep list of directories

I have multiple systems with each having its own log directory containing log file pertinent to that system. I have constructed a find/grep statement to get the list of directories ending in log, but I can't seem to pipe that to grep (via xargs) to have grep search each file in the log directories... (8 Replies)
Discussion started by: jmgibby
8 Replies

8. Shell Programming and Scripting

how to grep a file in list

hi all, for an example: In a file out.txt contains: /export/home/raghu/bin/debug_serverLog_apache_20090626_0625.txt How to grep or cut the value as "debug_serverLog_apache_20090626_0625.txt" or i want only the output as "debug_serverLog_apache_20090626_0625.txt" pls advice me (3 Replies)
Discussion started by: raghur77
3 Replies

9. Shell Programming and Scripting

grep a list of values

Hi everybody! :) :D :D :) it's great to be here since this is my first post. touch /base/oracle/FRA/XMUT00/RMAN_FLAG touch /base/oracle/FRA/XRLL00/RMAN_FLAG find directory name containing RMAN_FLAG : $ find /base/oracle/FRA -name RMAN_FLAG -print|xargs -n1 dirname |sort -u... (3 Replies)
Discussion started by: jolan_louve
3 Replies

10. UNIX for Dummies Questions & Answers

List grep results

Hi I need to search for matching strings in a database and I want to print out all files that matches in "detail", which means that I want the output to contain datum of last saving. I only get the grep function tp print the actual file names which is not enough since the database is to large... (14 Replies)
Discussion started by: slire
14 Replies
Login or Register to Ask a Question