简体   繁体   中英

how to get a sub string from line which is output of other grep command

I have file with data in multiple lines. I can find the lines by using grep command. I need to get the some portion of string from output of above grep command. And, also need to find any duplicates there.

Thanks.

You might want to use a combination of grep and cut like:

grep PATTERN FILENAME | cut -b x-z

where grep searches for your PATTERN in FILENAME and cut shows the output from byte x to z (these should be numbers).

Or you might use a single command:

awk '/PATTERN/ { print substr($0,FROM,LENGTH) }' FILENAME

in this version you need to replace FROM with the byte where the desired output starts and LENGTH with ... well you might guess it.

There are many Unix tools for string searching and manipulation, and the chain of grep , sed , awk , up to perl provides a (mostly compatible) progression in capabilities. So, you can start out with grep , and when that doesn't suit you, "upgrade" to sed , and so on. (The reason that the most powerful one hasn't fully driven out the weaker ones is the same like you don't use a big power drill just to put a small nail into a drywall.)

The best solution to your problem depends on the some portion of string details. If this is delimited by some character, or at fixed positions, cut can do that. To delete unwanted pattern matches, sed is the one. With the latter, you can grep and delete in one invocation, but it's simpler (and only a bit less efficient) to combine them via piping :

$ grep PATTERN file | sed 's/unwanted stuff//'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM