Help needed in unix commands


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help needed in unix commands
# 1  
Old 09-01-2010
Help needed in unix commands

i define a function outside the nawk command which trims the leading and trailing spaces like

Code:
  function TrimSpace(x)
  {
    y=gsub(/^[ \t]+|[ \t]+$/,"",x)
    {
      return x
    }
  }

and i want to call this function in nawk commad like.

Code:
 
nawk '{print TrimSpace($1)}' file

Note: this function works fine if i define it within nawk.You may use your any dumy function for testing.
If it is possible to call any outside function in nawk? and if yes,how to accomplish it

Moderator's Comments:
Mod Comment Your 2nd question has been removed since it is already an own thread in this forum. Either have it in here as 2nd question or have it as an own thread but not both. This is double posting which is not allowed to the rules of the forum. So your 2nd question has been stripped away by me - there is still your other thread with exactly the same question in it.

Last edited by zaxxon; 09-01-2010 at 03:57 AM..
# 2  
Old 09-01-2010
Better to define the function inside the awk.
something like:


Code:
$ cat y
test,   test2,   test3   ,test4
test,   test2,   test3   ,test4
test,   test2,   test3   ,test4
test,   test2,   test3   ,test4
$ 
$ 
$ 
$ cat mm
awk -F, '

function TrimSpace(mystr)
 {
  tstr=mystr;
  sub(/^[ \t\n]*/,"",tstr);
  sub(/[ \t\n]*$/,"",tstr);
  return tstr
 }

{print TrimSpace($2)}' y
$ 
$ 
$ 
$ ./mm
test2
test2
test2
test2
$

# 3  
Old 09-01-2010
Quote:
Originally Posted by anchal_khare
Better to define the function inside the awk.
something like:


Code:
$ cat y
test,   test2,   test3   ,test4
test,   test2,   test3   ,test4
test,   test2,   test3   ,test4
test,   test2,   test3   ,test4
$ 
$ 
$ 
$ cat mm
awk -F, '
 
function TrimSpace(mystr)
 {
  tstr=mystr;
  sub(/^[ \t\n]*/,"",tstr);
  sub(/[ \t\n]*$/,"",tstr);
  return tstr
 }
 
{print TrimSpace($2)}' y
$ 
$ 
$ 
$ ./mm
test2
test2
test2
test2
$

thanks for reply

but actually i want to know the possibility of calling an outside function within nawk
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Help needed - UNIX command

The 'env' command prints out all of the environment variables and their values. Come up with a command that prints a list of environment variables whose names end with either 'NAME', 'DIR' or 'PATH'. What I've tried. $ env | grep '' Although it highlights the part of the variables... (8 Replies)
Discussion started by: edujs7
8 Replies

2. Shell Programming and Scripting

Linux Commands needed for replacing variable number of spaces with a single , in a txt file

Hi I want to read a text file and replace various number of spaces between each string in to a single "," or any other character .Please let me know the command to do so. My input file is a txt file which is the output of a SQL table extract so it contains so many spaces between each column of the... (2 Replies)
Discussion started by: Hari Prasanth
2 Replies

3. Homework & Coursework Questions

Unix Homework Help needed

1 Petras Pavardenis 1980 5 08 Linas Bajoriunas 1970 10 3 Saulius Matikaitis 1982 2 5 Mindaugas Stulgis 1990 7 6 Rimas Nasickis 1964 10 7 ... (1 Reply)
Discussion started by: vaidastf
1 Replies

4. Solaris

Help needed - trying to run commands in Guest LDoms from Control LDOM

Hi Folks, I am used to writing scripts to get info by running commands at local zones level from their respective global zone by using zlogin <localzone> "command>" while remaining at the global zone level. Can the same be done with Guest LDoms while remaining at the control LDOM level? ... (4 Replies)
Discussion started by: momin
4 Replies

5. Shell Programming and Scripting

Help needed in some unix commands

Hi, Solutions may be in ksh or bash 1. i have some files like. CAT_CO00102_20100816190955.Z CAT_CO00102_20100816190955Q.Z CAT_CO00202_20100825135947A.Z to find out files ends with A.Z or Q.Z i use following in ksh. CAT="CAT*.Z" ls $CAT CAT_CO00102_20100816190955Q.Z ... (5 Replies)
Discussion started by: malikshahid85
5 Replies

6. UNIX for Dummies Questions & Answers

Running UNIX commands remotely in Windows box from Unix box – avoid entering password

I am able to run the UNIX commands in a Windows box from a UNIX box through "SSH" functionality. But whenever the SSH connection is established between UNIX and Windows, password for windows box is being asked. Is there a way to avoid asking password whenever the SSH connection is made? Can I... (1 Reply)
Discussion started by: D.kalpana
1 Replies

7. Shell Programming and Scripting

unix manual needed :)

hi guys... am new 2 dis unix world... am in need of a unix manual... cud sum1 pls post sum links 2 download it?>?>? :confused: Danks in advance... ;) ;) ;) (1 Reply)
Discussion started by: sundar_shankar
1 Replies

8. UNIX for Dummies Questions & Answers

Unix help needed !!

friends i wanted to now whehter there exists a book as a solution to the questions posted in exercies of the Design of Unix Operating system by Maurice J Bach !..its really urgent !! (1 Reply)
Discussion started by: darshaan
1 Replies

9. UNIX for Dummies Questions & Answers

Maingrame to UNIX sending UNIX commands

I want to know if there is a way to send unix commands thru FTP from a mainframe to kick off Autosys Jobs. I just need to send a command from the mainframe to UNIX and have UNIX execute that command. (2 Replies)
Discussion started by: skammer
2 Replies

10. UNIX for Dummies Questions & Answers

Unix help needed!

I have a few questions about Unix. Can you please help! 1.How do I create an alias that greps the password file for my username and returns my password file entry? Once I get the alias to work I want to be able to let a friend use it .So that he can use the alias to locate his password entry... (1 Reply)
Discussion started by: JJJ
1 Replies
Login or Register to Ask a Question