# Fetch Valid String

**URL:** <https://community.unix.com/t/fetch-valid-string/337390>\
**Category:** Shell Programming and Scripting\
**Created:** [December 5, 2013, 9:13am UTC](https://community.unix.com/t/fetch-valid-string/337390 "2013-12-05T09:13:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![suresh\_target](https://community.unix.com/letter_avatar/suresh_target/32/6_22734026db12803ebb3e059622ac3534.png) [@suresh\_target](https://community.unix.com/u/suresh_target)\
**Post date:** [December 5, 2013, 9:13am UTC](https://community.unix.com/t/fetch-valid-string/337390/1 "2013-12-05T09:13:12Z")

</div>

Hi All,

How to fetch the particular row based on my search command. For example

Source Column

```nohighlight
Column A Column B
 
001,56123456
002,57123456
003,123456

```

Expected Output:

```nohighlight
003,123456

```

To get this output I tried below mentioned function

```nohighlight
grep '123456' filename.txt

```

but I got all three rows. I need to fetch only the particular rows from my source file. Please guide me how can I do that.

Thanks

Suresh

---

<div class="post-metadata">

**Author:** ![Akshay\_Hegde](https://community.unix.com/user_avatar/community.unix.com/akshay_hegde/32/2771_2.png) [@Akshay\_Hegde](https://community.unix.com/u/Akshay_Hegde)\
**Post date:** [December 5, 2013, 9:22am UTC](https://community.unix.com/t/fetch-valid-string/337390/2 "2013-12-05T09:22:24Z")

</div>

```nohighlight
$ awk -F, '$2 == search' search="123456" filename.txt

```

---

<div class="post-metadata">

**Author:** ![rdrtx1](https://community.unix.com/user_avatar/community.unix.com/rdrtx1/32/2721_2.png) [@rdrtx1](https://community.unix.com/u/rdrtx1)\
**Post date:** [December 5, 2013, 9:57am UTC](https://community.unix.com/t/fetch-valid-string/337390/3 "2013-12-05T09:57:42Z")

</div>

try also:

```nohighlight
sed -n '/\b123456\b/p' infile

```

---

<div class="post-metadata">

**Author:** ![brianadams](https://community.unix.com/letter_avatar/brianadams/32/6_22734026db12803ebb3e059622ac3534.png) [@brianadams](https://community.unix.com/u/brianadams)\
**Post date:** [December 5, 2013, 11:46am UTC](https://community.unix.com/t/fetch-valid-string/337390/4 "2013-12-05T11:46:12Z")

</div>

```nohighlight
#!/usr/bin/env perl
use strict;
use warnings;

while(<>){
	my @sp = split(/,/ , $_);	
	print if $sp[1] == 123456; 
}

```

---

<div class="post-metadata">

**Author:** ![suresh\_target](https://community.unix.com/letter_avatar/suresh_target/32/6_22734026db12803ebb3e059622ac3534.png) [@suresh\_target](https://community.unix.com/u/suresh_target)\
**Post date:** [December 6, 2013, 12:43am UTC](https://community.unix.com/t/fetch-valid-string/337390/5 "2013-12-06T00:43:11Z")

</div>

Really thanks to everyone for your right time help
