AWK command working different in Linux


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK command working different in Linux
# 1  
Old 11-23-2011
AWK command working different in Linux

Hi All
I have fired a command in linux
Code:
table=`echo ${file_name} | awk '{FS="/"; print $NF}' | awk '{FS="."; print $1}'`

where file_name has
/data/ds/dpr_ebicm_uat/backfill/temp/etl_app_info.csv
/data/ds/dpr_ebicm_uat/backfill/temp/etl_app_jobs.csv
/data/ds/dpr_ebicm_uat/backfill/temp/etl_ctrlmrmv_config.csv
/data/ds/dpr_ebicm_uat/backfill/temp/etl_filerename_config.csv
/data/ds/dpr_ebicm_uat/backfill/temp/etl_fileutil_config.csv
/data/ds/dpr_ebicm_uat/backfill/temp/etl_filewatcher_config.csv
/data/ds/dpr_ebicm_uat/backfill/temp/etl_formatter_config.csv

when i do

echo $table i get following value
/data/ds/dpr_ebicm_uat/backfill/temp/purge_table_config.csv

whereas i need only purge_table_config which refers to the table name.

But the same command gives me the correct output in unix. But now as our project is migrating we are using linux.
Could you all please help me to modify this command so that it gives the output as only purge_table_config which is only the table name.

Thanks
# 2  
Old 11-23-2011
awk on many Linux distros is actually gawk.

gawk splits the line into fields when the line is read - resetting FS afterwards (i.e. inside a procedure) doesn't change the field splitting (which is correct POSIX, iirc).

You can either set FS inside BEGIN, or use -F.
e.g.
Code:
table=`echo ${file_name} | awk -F"/" '{print $NF}' | awk -F"." '{print $1}'`


Last edited by CarloM; 11-23-2011 at 08:58 AM..
# 3  
Old 11-23-2011
With one awk command:
Code:
table=`echo ${file_name} | awk -F"[./]" '{print $(NF-1)}'

# 4  
Old 11-23-2011
Interpretation of awk command using index field in linux

Thanks it worked,
I have another question
Could you please tell me the interpretation of following command as to what output would it give me in linux
Code:
echo "${VALUE}" | awk '{print index($0,"*")}'` -gt 0 ] && VALUE=`echo ${VALUE} | sed -e 's/"//g'

where VALUE=/data/ds/dpr_ebicm_uat/backfill/temp/etl_app_jobs.csv
I ran the command and it is giving me output as
0
0
0 likewise all zeros
I also need interpretation of following command
Code:
cat ${file_name} | awk 'NR>1' | while read data
value=`echo ${data} | awk '{if (substr($1,0,1) == "\047") gsub("^\047+|\047+$", "", $0); gsub("\047,\047"," ",$0); print}'`
i want to know what value will the value variable have.
 
where file_name contains the list of files mentioned earlier
/data/ds/dpr_ebicm_uat/backfill/temp/etl_app_info.csv 
/data/ds/dpr_ebicm_uat/backfill/temp/etl_app_jobs.csv 
/data/ds/dpr_ebicm_uat/backfill/temp/etl_ctrlmrmv_config.csv 
/data/ds/dpr_ebicm_uat/backfill/temp/etl_filerename_config.csv 
/data/ds/dpr_ebicm_uat/backfill/temp/etl_fileutil_config.csv 
/data/ds/dpr_ebicm_uat/backfill/temp/etl_filewatcher_config.csv 
/data/ds/dpr_ebicm_uat/backfill/temp/etl_formatter_config.csv

Thanks
# 5  
Old 11-23-2011
Part of the command appears to be missing? Before ` -gt 0 ]
# 6  
Old 11-23-2011
Actually its the same

like
Code:
 
[ `echo "${VALUE}" | awk '{print index($0,"*")}'` -gt 0 ] && VALUE=`echo ${VALUE} | sed -e 's/"//g'`

I have taken this directly from the script, when i tried running it on linux its throwing me following error
-bash: 0: command not found
I also tried running this command separately VALUE=`echo ${VALUE} | sed -e 's/"//g'` it again throws a error like
-bash: command not found. Do these commands have a separate syntax in linux.
Please help me out with this. As the script is working different in linux enviornment

Last edited by vee_789; 11-23-2011 at 10:38 AM..
# 7  
Old 11-23-2011
Quote:
Originally Posted by vee_789
Actually its the same
No, it isn't - the characters you previously missed off the front are rather important, since otherwise it's not valid syntax...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Simple sed command not working; could be a Mac/Linux vs. PC/Linux issue

Hello, I am on a Mac and trying to clean up some monthly files with a very simple SED: sed '3,10d;/<ACROSS>/,$d' input.txt > output.txt (from the input, delete lines 3 - 10; then delete from the line containing <ACROSS> to the end of the file) then output to output.txt Even when I try... (2 Replies)
Discussion started by: verbatim
2 Replies

2. Shell Programming and Scripting

At command for Linux job not working from php

I have a script .sh with a curl command in it: curl --digest "http://xxxxxxx.xxx/mail.php?email=xxxx@xxx.xx"; from terminal this works good : ./script.sh //(mail arrives in my box) from php this works good: shell_exec('./phpsc.sh'); // (mail arrives in my box) from terminal this... (13 Replies)
Discussion started by: extra93
13 Replies

3. Shell Programming and Scripting

Awk: System command not working in awk

Hi, I have around 10 files in a folder in which I want to change the file format from tab(\t) to pipe(|) with some changes in the fields as well. Below is the code, while tmp file is getting generated but move command is not working, please help Following is the code awk -F"\t" '{print... (2 Replies)
Discussion started by: siramitsharma
2 Replies

4. UNIX for Dummies Questions & Answers

SWAP command not working on Linux machine

Hi I am working on linux machine and swap command is not working Linux Machine On Solaris machine it is working fine: uname -a SunOS rgsm01 5.9 Generic_118558-03 sun4u sparc SUNW,Sun-Fire-V440 swap -s total: 6596320k bytes allocated + 1035968k reserved = 7632288k used, 38893408k available ... (5 Replies)
Discussion started by: Basant Mishra
5 Replies

5. Red Hat

isql command not working in Linux 64bit

I am using Linux RHEL5 64 bit and installed oracle 11g. I want to check ODBC connection , i had modified odbc.ini file when i use this command -isql I am getting following error . bash: isql: command not found Kindly help (5 Replies)
Discussion started by: roopalidalvi231
5 Replies

6. Shell Programming and Scripting

awk - System command not working

dear All, my awk system command isn't working or rather I'm missing something in my command. Appreciated , if anyone can assist me what exactly I'm missing ?? awk ' /^/ { > c=split($3,a,"/") ;for(n=1; n<=c; ++n) > { > if (system("test -d" /home/cubedata/20120104/"$1"/"a")) { > print... (5 Replies)
Discussion started by: manas_ranjan
5 Replies

7. Shell Programming and Scripting

sed command working different in linux environment.

Hi I tried running the code scrname=`whence $0 | sed -e 's/\.\///g'` where $0 is substituted by cm_dsjobrun.sh in unix env then the value it returns me is SCRNAME=/data/ds/dpr_ebicm_uat/etl/cm3_0/scripts/shell/cm_dsjobrun.sh whereas i ran the same code on linux env The value... (9 Replies)
Discussion started by: vee_789
9 Replies

8. Shell Programming and Scripting

ITcl :: try_eval command not working in Linux 5.4

Hi, My code uses try_eval block to do some code execution. This piece of code does not work on RHEL 5.4. The program just hangs at the try_eval block and does not throw any errors either. The same program however works in other Linux boxes that we have. I have written a simple program that... (0 Replies)
Discussion started by: archana485
0 Replies

9. Shell Programming and Scripting

awk command not working

Hi all, Trying to write a script that reads a file and prints everything after a certain string is found to the end of the file. Awk is giving me an error and not sure why it doesn't work: # cat test_file Mon Nov 16 2009 16:11:08 abc def Tue Nov 17 2009 16:08:06 ghi jkl Wed Nov 18... (8 Replies)
Discussion started by: jamie_collins
8 Replies

10. Programming

cos() command not working in Linux

Hi, I have written a c program to find cos() of a value , its not working, I am getting value of "val " as 0000. #include<stdio.h> #include<math.h> main() { float val; val = cosf( 1.570796); printf("\nval = %f",val); } (9 Replies)
Discussion started by: shashi
9 Replies
Login or Register to Ask a Question