awk and split and variable used prior


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk and split and variable used prior
# 1  
Old 11-08-2005
awk and split and variable used prior

I am trying to use awk and its split function to get the number of tokens in a string that are seperated by underscores.

ex_alex_is_testing_this_script_ex would return 7. It works when I directly put the string in. However, I can not get it to work when I try to refer to a variable used earlier in my Script. I think this is because of the BEGIN statement. Any ideas to get this to work with a variable that was used prior.


WORKING:
'BEGIN {print split("ex_alex_is_testing_this_script_ex", myarray, "_")}'
--> returns 7

NOT WORKING
'BEGIN {print split($something, myarray, "_")}' i.e --> I want to count how many underscores are in the $something.

Thanks for your help,

Alex
Montreal, Canada
# 2  
Old 11-08-2005
Code:
echo "${something}" | nawk -F_ 'BEGIN {print NF}'
#or
nawk -v something=${something}" 'BEGIN {print split(something, foo, "_")}' < /dev/null
#or
nawk -v something=${something}" 'BEGIN {print gsub("_", "", something)}' < /dev/null
#or
echo "${something}" | sed 's/[^_]//g' | wc -c


Last edited by vgersh99; 11-08-2005 at 05:03 PM..
# 3  
Old 11-08-2005
Works great

thanks for you help. I'm using the second example.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to split one field and print the last two fields within the split part.

Hello; I have a file consists of 4 columns separated by tab. The problem is the third fields. Some of the them are very long but can be split by the vertical bar "|". Also some of them do not contain the string "UniProt", but I could ignore it at this moment, and sort the file afterwards. Here is... (5 Replies)
Discussion started by: yifangt
5 Replies

2. Shell Programming and Scripting

[Perl] Split lines into array - variable line items - variable no of lines.

Hi, I have the following lines that I would like to see in an array for easy comparisons and printing: Example 1: field1,field2,field3,field4,field5 value1,value2,value3,value4,value5Example 2: field1,field3,field4,field2,field5,field6,field7... (7 Replies)
Discussion started by: ejdv
7 Replies

3. Shell Programming and Scripting

using awk for setting variable but change the output of this variable within awk

Hi all, Hope someone can help me out here. I have this BASH script (see below) My problem lies with the variable path. The output of the command find will give me several fields. The 9th field is the path. I want to captured that and the I want to filter this to a specific level. The... (6 Replies)
Discussion started by: Cowardly
6 Replies

4. Shell Programming and Scripting

How to split a data assigned to a variable

The requirement is, there is a log file which contains a huge data. i need to get a particular field out of it by searching with another field. ex: 2011-03-28 13:00:07,423 : millis=231 q={ call get_data_account(?,?,?,?,?) }, params= i need to search for the word "get_data_account" in file... (1 Reply)
Discussion started by: Jassz
1 Replies

5. Shell Programming and Scripting

Split variable length and variable format CSV file

Dear all, I have basic knowledge of Unix script and her I am trying to process variable length and variable format CSV file. The file length will depend on the numbers of Earnings/Deductions/Direct Deposits. And The format will depend on whether it is Earnings/Deductions or Direct Deposits... (2 Replies)
Discussion started by: chechun
2 Replies

6. Shell Programming and Scripting

Split one Variable into three

Hi i have a variable $VAR =15 14 13, i want to split this number and store in 3 variables say $N1=15 $N2=14 $N3=13 i know its simple but my mind is not giving me answers at this point of time. i will be running the code in Solaris Box..please help (3 Replies)
Discussion started by: Shellslave
3 Replies

7. Shell Programming and Scripting

split a variable into two

Hi, I have a variable with the value "hello world". I want to split this variable into two, so that i can write "hello" into one variable and "world" into another. Any idea how to do this? Thanks, Sri (11 Replies)
Discussion started by: srilaxmi
11 Replies

8. Shell Programming and Scripting

split variable values into array

i have these values inside variable $blah BUNGA TERATAI 3 5055 ITH 1 0 0 0 1 1 JADE TRADER 143W ITH 4 0 0 0 4 4 MOL SPLENDOR 0307A ITH 3 0 0 0 3 3 so how do I split them into array with the... (4 Replies)
Discussion started by: finalight
4 Replies

9. UNIX for Dummies Questions & Answers

Split a file with no pattern -- Split, Csplit, Awk

I have gone through all the threads in the forum and tested out different things. I am trying to split a 3GB file into multiple files. Some files are even larger than this. For example: split -l 3000000 filename.txt This is very slow and it splits the file with 3 million records in each... (10 Replies)
Discussion started by: madhunk
10 Replies
Login or Register to Ask a Question