Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep to search data in first column

Tags:

linux

grep

ubuntu

I have a text file with two columns.

Product   Cost
Abc....def  10
Abc.def     20
ajsk,,lll   04

I want to search for product starts from "Abc" and ends with "def" then for those entries I want to add Cost. I have used :

grep "^Abc|def$" myfile 

but it is not working

like image 931
Aquarius24 Avatar asked Sep 14 '15 00:09

Aquarius24


People also ask

How do I AWK my first column?

awk to print the first column. The first column of any file can be printed by using $1 variable in awk. But if the value of the first column contains multiple words then only the first word of the first column prints. By using a specific delimiter, the first column can be printed properly.

How do I search for something using grep?

The grep command searches through the file, looking for matches to the pattern specified. To use it type grep , then the pattern we're searching for and finally the name of the file (or files) we're searching in.

How do I get the first column of a file in Unix?

The `awk` command can be applied in different ways to get the first column and/or last column from any command output or from tabular data. It is important to note that a field separator is required in the command, and if one is not provided, then the space is used.


1 Answers

Use awk. cat myfile | awk '{print $1}' | grep query

like image 162
Heman Gandhi Avatar answered Sep 28 '22 18:09

Heman Gandhi