# awk command with PERL

**URL:** <https://community.unix.com/t/awk-command-with-perl/213737>\
**Category:** Shell Programming and Scripting\
**Created:** [September 24, 2008, 6:41am UTC](https://community.unix.com/t/awk-command-with-perl/213737 "2008-09-24T06:41:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kmkbuddy\_1983](https://community.unix.com/letter_avatar/kmkbuddy_1983/32/6_449c533f586939b6e5f246632a46421f.png) [@kmkbuddy\_1983](https://community.unix.com/u/kmkbuddy_1983)\
**Post date:** [September 24, 2008, 6:41am UTC](https://community.unix.com/t/awk-command-with-perl/213737/1 "2008-09-24T06:41:08Z")

</div>

Hi All,

My Input file looks like below:

Input:  
100,200,300

$fw=`head -1 test.csv | awk -F, '{print \$1}'`;  
$fw="'$fw"  
$fw="$fw'"  
print $fw

Output:'100'

I want the first field to be printed in single quotes ('') like above.  
I could get the ouptput but the problem is single quote (') is printed in next line below.  
'100  
'  
I dont know how to avoid this space.

---

<div class="post-metadata">

**Author:** ![era](https://community.unix.com/letter_avatar/era/32/6_449c533f586939b6e5f246632a46421f.png) [@era](https://community.unix.com/u/era)\
**Post date:** [September 24, 2008, 6:52am UTC](https://community.unix.com/t/awk-command-with-perl/213737/2 "2008-09-24T06:52:30Z")

</div>

To remove a trailing newline in Perl, use chomp

---

<div class="post-metadata">

**Author:** ![ghostdog74](https://community.unix.com/letter_avatar/ghostdog74/32/6_449c533f586939b6e5f246632a46421f.png) [@ghostdog74](https://community.unix.com/u/ghostdog74)\
**Post date:** [September 24, 2008, 6:57am UTC](https://community.unix.com/t/awk-command-with-perl/213737/3 "2008-09-24T06:57:33Z")

</div>

why use awk/head when you can do all in Perl??

---

<div class="post-metadata">

**Author:** ![era](https://community.unix.com/letter_avatar/era/32/6_449c533f586939b6e5f246632a46421f.png) [@era](https://community.unix.com/u/era)\
**Post date:** [September 24, 2008, 7:01am UTC](https://community.unix.com/t/awk-command-with-perl/213737/4 "2008-09-24T07:01:43Z")

</div>

Why use either when you can do it all in shell?

```nohighlight
IFS=, read first others <file.csv
echo "'$first'"

```

---

<div class="post-metadata">

**Author:** ![kmkbuddy\_1983](https://community.unix.com/letter_avatar/kmkbuddy_1983/32/6_449c533f586939b6e5f246632a46421f.png) [@kmkbuddy\_1983](https://community.unix.com/u/kmkbuddy_1983)\
**Post date:** [September 24, 2008, 7:31am UTC](https://community.unix.com/t/awk-command-with-perl/213737/5 "2008-09-24T07:31:17Z")

</div>

> [@ghostdog74](#):
>
> why use awk/head when you can do all in Perl??

I want to capture first field in a file and check for some conditions. File has got 10 million records.

---

<div class="post-metadata">

**Author:** ![ghostdog74](https://community.unix.com/letter_avatar/ghostdog74/32/6_449c533f586939b6e5f246632a46421f.png) [@ghostdog74](https://community.unix.com/u/ghostdog74)\
**Post date:** [September 24, 2008, 7:45am UTC](https://community.unix.com/t/awk-command-with-perl/213737/6 "2008-09-24T07:45:58Z")

</div>

```nohighlight
perl -e '@a=split(/ +/, <>);print $a[0]' file

```

---

<div class="post-metadata">

**Author:** ![era](https://community.unix.com/letter_avatar/era/32/6_449c533f586939b6e5f246632a46421f.png) [@era](https://community.unix.com/u/era)\
**Post date:** [September 24, 2008, 2:57pm UTC](https://community.unix.com/t/awk-command-with-perl/213737/7 "2008-09-24T14:57:11Z")

</div>

You want to split on comma /,/ not whitespace.

The number of lines doesn't really matter if you only read the first line. All of the proposed solutions I believe have this property.
