Awk split doesn't work for empty delimiter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk split doesn't work for empty delimiter
# 1  
Old 12-30-2008
Awk split doesn't work for empty delimiter

Does anyone know how will I make awk's split work with empty or null separator/delimiter?

Code:
echo ABCD | awk '{ split($0,arr,""); print arr[1]; }'

I need output like:
HTML Code:
A
B
C
D
I am under HP-UX
# 2  
Old 12-30-2008
Hi,

try:

Code:
echo ABCD | awk '{ gsub(/./,"&\n",$0); print $0 }'

Output:
Code:
A
B
C
D

HTH Chris
# 3  
Old 12-30-2008
Sorry. I forgot to mentioned that I need to put each character in an array or variable, the reason I use the split function of awk.
# 4  
Old 12-30-2008
Code:
Code:
echo ABCD | awk '{ split($0,a,""); for (i in a){print a[i]} }'

Output:
Code:
D
A
B
C

# 5  
Old 12-30-2008
To put in an array in awk-

echo ABCD | awk ' { for ( i = 1; i <= length($0); i++ ) a[i]=substr($0,i,1) } END { for ( i in a ) print a[i] } '

B
C
D
A
# 6  
Old 01-05-2009
Thanks a bunch! The code from pmm works but the one from Christoph Spohr did not. Im not sure why. Anyways, thanks again!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert a value between two empty delimiter in the file.

Would like to insert value between two empty delimiter and at the very last too if empty. $ cat customerleft.tbl 300|Customer#000000300|I0fJfo60DRqQ|7|17-165-193-5964|8084.92|\N|p fluffily among the slyly express grouches. furiously express instruct||||||||||||||||||||||||\N... (3 Replies)
Discussion started by: Mannu2525
3 Replies

2. Shell Programming and Scripting

Why my awk doesn't work?

root@SDP_Wildcat_Pass-3-C1:~# cat /proc/driver/rtc rtc_time : 05:29:40 rtc_date : 2014-12-19 alrm_time : 01:51:53 alrm_date : 2014-12-20 alarm_IRQ : no alrm_pending : no update IRQ enabled : no periodic IRQ enabled : no periodic IRQ... (4 Replies)
Discussion started by: yanglei_fage
4 Replies

3. Shell Programming and Scripting

split file by delimiter with csplit

Hello, I want to split a big file into smaller ones with certain "counts". I am aware this type of job has been asked quite often, but I posted again when I came to csplit, which may be simpler to solve the problem. Input file (fasta format): >seq1 agtcagtc agtcagtc ag >seq2 agtcagtcagtc... (8 Replies)
Discussion started by: yifangt
8 Replies

4. Shell Programming and Scripting

How to split a string with no delimiter

Hi; I want to write a shell script that will split a string with no delimiter. Basically the script will read a line from a file. For example the line it read from the file contains: 99234523 These values are never the same but the length will always be 8. How do i split this... (8 Replies)
Discussion started by: saint34
8 Replies

5. Shell Programming and Scripting

HELP: If Doesn't Work in AWK

Hi! I have a somehow big file (almost 3000 lines long and thirteen columns). Some lines have no value at all or, at least, are incomplete. The columns' values that have no data are marked with a "-" and the corresponding line (the line that owns that value) should be discarded and not used. ... (5 Replies)
Discussion started by: Marcelo de Brit
5 Replies

6. Shell Programming and Scripting

renaming files using split with a delimiter

I have a directory of files that I need to rename by splitting the first and second halves of the filenames using the delimiter "-O" and then renaming with the second half first, followed by two underscores and then the first half. For example, natfinal1995annvol1_14.pdf -O filenum-20639 will be... (2 Replies)
Discussion started by: swimulator
2 Replies

7. Shell Programming and Scripting

Awk -- why doesn't my min work?

So, I have a files with entries in this format: servername,username,sub_username,useless_datapoint,mail_size So, a few sample lines: server_a,bob,jane,useless,112351 server_a,bob,jim,useless,421193 server_a,bob,bob,useless,0029385 server_a,karen,will,useless,112351... (3 Replies)
Discussion started by: treesloth
3 Replies

8. Shell Programming and Scripting

Awk: Can anyone tell me why this doesn't work?

If there exists a field in stdin, print it, otherwise, print hello..... These print nothing: cat /dev/null | awk '{if ( length > 0 ) print $1; else print "hello"}' cat /dev/null | awk '{if ( $1 ) print $1; else print "hello"}'But the scripts work if I run them directly in a terminal: ... (8 Replies)
Discussion started by: ksheller
8 Replies

9. Shell Programming and Scripting

awk -v -- Why doesn't my example work?

Hi. I've been playing around a bit. This isn't for any practical purpose-- it's really just a theoretical exercise. I wrote this little thing: foreach num ( 6 5 4 ) awk -v "number=$num" 'BEGIN{for(x=0;x<$number;x++) printf "-"; printf "\n"}' end I would expect the following output: ... (3 Replies)
Discussion started by: treesloth
3 Replies

10. UNIX for Dummies Questions & Answers

Script doesn't work, but commands inside work

Howdie everyone... I have a shell script RemoveFiles.sh Inside this file, it only has two commands as below: rm -f ../../reportToday/temp/* rm -f ../../report/* My problem is that when i execute this script, nothing happened. Files remained unremoved. I don't see any error message as it... (2 Replies)
Discussion started by: cheongww
2 Replies
Login or Register to Ask a Question