awk command with PERL


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk command with PERL
# 1  
Old 09-24-2008
awk command with PERL

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.
# 2  
Old 09-24-2008
To remove a trailing newline in Perl, use chomp
# 3  
Old 09-24-2008
why use awk/head when you can do all in Perl??
# 4  
Old 09-24-2008
Why use either when you can do it all in shell?

Code:
IFS=, read first others <file.csv
echo "'$first'"

# 5  
Old 09-24-2008
Quote:
Originally Posted by 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.
# 6  
Old 09-24-2008
Code:
perl -e '@a=split(/ +/, <>);print $a[0]' file

# 7  
Old 09-24-2008
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using awk command in Perl

Hi All, I got some requirement of using Awk in perl subroutine One of the variable in my perl script holds below type of data. I want to split this value as quotes seperated and get the second last field i.e., 3206000 from above. I used below code in my perl sub-routine,... (3 Replies)
Discussion started by: hi.villinda
3 Replies

2. Shell Programming and Scripting

Sed/awk/perl command to replace pattern in multiple lines

Hi I know sed and awk has options to give range of line numbers, but I need to replace pattern in specific lines Something like sed -e '1s,14s,26s/pattern/new pattern/' file name Can somebody help me in this.... I am fine with see/awk/perl Thank you in advance (9 Replies)
Discussion started by: dani777
9 Replies

3. Shell Programming and Scripting

Convert awk command to Perl

Hi all; I have a Perl script that uses this awk command wrapped in a "system" call; it works exactly as I want it; however I want to keep this a Perl script and have very few if any "system" command...so the question; can someone help me code this in Perl please ... i have tried an now I am... (1 Reply)
Discussion started by: gvolpini
1 Replies

4. Programming

Errors in Perl when using awk command

Hi Guys, Hope everyone is fine :) I have this code below: #!/usr/bin/perl $num_of_files=`ls | grep -v remover | wc -l`; $remover=`ls -lrt | grep -v total | grep -v remover | head -1 | awk '{print $8}' | rm \`xargs\``; if ($num_of_files>3) { system ($remover); } When I... (3 Replies)
Discussion started by: rymnd_12345
3 Replies

5. Shell Programming and Scripting

using awk in perl with split command

Hi, I have an array with following data. First field shows the owner and second is unique name. Now i have to pic the latest value with respect to the date in case of duplicate. like "def" is from two owners "rahul/vineet", now i want the latest from the two and the owner name also for all the... (9 Replies)
Discussion started by: vineet.dhingra
9 Replies

6. Shell Programming and Scripting

usage of AWK command under perl script

i have two files as shown below t1.txt: argument1 argu2 argu37 t2.txt: 22 33 44 i want o/p as argument1 22 argu2 33 argu37 44 i am trying to merge two file under perl script using following system("paste t1.txt t2.txt | awk... (3 Replies)
Discussion started by: roopa
3 Replies

7. Programming

Executing a awk command inside perl script --

Hello experts I want to execute a awk command, which reads from txt files and sums the numbers from the first column for those listed only inside a <init> block -- The awk command is like awk '/<\/?init>/{x = !x}x{a++}x && a > 2{sum+=$1}END{printf"%E" "\n", sum} So, I want to execute... (2 Replies)
Discussion started by: Alkass
2 Replies

8. Shell Programming and Scripting

Command to remove duplicate lines with perl,sed,awk

Input: hello hello hello hello monkey donkey hello hello drink dance drink Output should be: hello hello monkey donkey drink dance (9 Replies)
Discussion started by: cola
9 Replies

9. Shell Programming and Scripting

awk command in script gives error while same awk command at prompt runs fine: Why?

Hello all, Here is what my bash script does: sums number columns, saves the tot in new column, outputs if tot >= threshold val: > cat getnon0file.sh #!/bin/bash this="getnon0file.sh" USAGE=$this" InFile="xyz.38" Min="0.05" # awk '{sum=0; for(n=2; n<=NF; n++){sum+=$n};... (4 Replies)
Discussion started by: catalys
4 Replies

10. Shell Programming and Scripting

awk/sed/perl command to delete specific pattern and content above it...

Hi, Below is my input file: Data: 1 Length: 20 Got result. Data: 2 Length: 30 No result. Data: 3 Length: 20 (7 Replies)
Discussion started by: edge_diners
7 Replies
Login or Register to Ask a Question