Using awk (or an alternative)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using awk (or an alternative)
# 1  
Old 03-08-2007
Using awk (or an alternative)

Hi,

I'm trying to echo a variable that has values separated by colons, putting each value on a new line. So as an example, a variable I might have would contain:

My name is Earl:My name is Dorothy:My name is Bernard:

And I want the output:

My name is Earl
My name is Dorothy
My name is Bernard

Also, I will be unaware of how many values the colon separated variable will contain (as it is generated by another function), so whatever is used will need to be flexible enough to accommodate any number of values. I'm not very competent in awk, so is there a way this can be done either with awk or with something else?

Also, is it possible to specify a field separator (such as a colon or comma) to the for command?
# 2  
Old 03-08-2007
Code:
echo "name is Earl:My name is Dorothy:My name is Bernard:" | tr ':' '\n'

or
Code:
echo "name is Earl:My name is Dorothy:My name is Bernard:" | sed "s/:/\\
/g"

or
Code:
echo "name is Earl:My name is Dorothy:My name is Bernard:" | awk ' gsub( ":" , "\n" ,$0 ) '

# 3  
Old 03-08-2007
or alternatively with 'nawk':
Code:
echo 'name is Earl:My name is Dorothy:My name is Bernard:' | nawk -v RS=: '$1=$1'

# 4  
Old 03-08-2007
if you have Python, here's an alternative:
Code:
#!/usr/bin/python
s = "name is Earl:My name is Dorothy:My name is Bernard:"
for item in s.split(":"):
    print item

# 5  
Old 03-08-2007
Code:
echo 'name is Earl:My name is Dorothy:My name is Bernard:' | awk -F":" -v OFS="\n" '$1=$1'

# 6  
Old 03-08-2007
Quote:
Originally Posted by michaeltravisuk
Hi,

I'm trying to echo a variable that has values separated by colons, putting each value on a new line. So as an example, a variable I might have would contain:

My name is Earl:My name is Dorothy:My name is Bernard:

And I want the output:

My name is Earl
My name is Dorothy
My name is Bernard

Also, I will be unaware of how many values the colon separated variable will contain (as it is generated by another function), so whatever is used will need to be flexible enough to accommodate any number of values. I'm not very competent in awk, so is there a way this can be done either with awk or with something else?

Also, is it possible to specify a field separator (such as a colon or comma) to the for command?
To answer your last question first: yes, set IFS; see my code below.

You don't need awk:

Code:
var="My name is Earl:My name is Dorothy:My name is Bernard:"
set -f
IFS=:
printf "%s\n" $var

A slower method would be to use tr:

Code:
printf "%s\n" "${var%:}" | tr : '\n'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Tip: alternative for NR==FNR in awk

Example: $ cat file1 2 3$ cat file2 1 2 3 4 5 6The following awk script works like a charm, NR==FNR is true for file1, the remainder runs for file2: awk ' NR==FNR {A; next} ($1 in A) ' file1 file2 2 3Now have an empty file1: >file1and run the awk script again. The result is empty... (8 Replies)
Discussion started by: MadeInGermany
8 Replies

2. Shell Programming and Scripting

Alternative for goto

#!/bin/sh label: echo sql poll v=`sqlplus -s <<! HR/HR set pages 0 echo off feed off select distinct status from emp where id=5; ! ` echo $v; echo it comes here after false if then echo if condition true sqlplus -l scott/tiger <<EOF select * from department; EXIT (2 Replies)
Discussion started by: kumaar1986
2 Replies

3. Shell Programming and Scripting

Making a faster alternative to a slow awk command

Hi, I have a large number of input files with two columns of numbers. For example: 83 1453 99 3255 99 8482 99 7372 83 175 I only wish to retain lines where the numbers fullfil two requirements. E.g: =83 1000<=<=2000 To do this I use the following... (10 Replies)
Discussion started by: s052866
10 Replies

4. Solaris

vi alternative

Is there any other editor, installed by 'default' in Sparc Solaris10, besides vi? I'd like to avoid installing anything new. If not, how to make vi more user-friendly? thanks. (8 Replies)
Discussion started by: orange47
8 Replies

5. Shell Programming and Scripting

Alternative for wc -l

Hi techies .. This is my first posting hr .. Am facing a serious performance problem in counting the number of lines in the file. The input files i get will be in some 10 to 15 Gb of size or even sometimes more ..and I will load it to db I have used wc -l to confirm whether the loader... (14 Replies)
Discussion started by: rajesh_2383
14 Replies

6. Shell Programming and Scripting

Using seq (Or alternative)

I usually just browse the forum/google for answers, however I've been stuck on a problem for a number of hours now and I've decided to join up and actually ask I've searched the forum ad naseum in an attempt to find answer to my query, however so far I have been unsuccessful. I'm no expert... (3 Replies)
Discussion started by: gtc
3 Replies

7. HP-UX

alternative for egrep -o on HP-UX

Hello to all board members!! I have a problem on a HP-UX system. I should write a script. Therefore I need to search after IP addresses in the output of a command. On Debian this works: ifconfig | egrep -o "{1,3}\.{1,3}\.{1,3}\.{1,3}" The script where i need this is not ifconfig, but... (2 Replies)
Discussion started by: vostro
2 Replies

8. Shell Programming and Scripting

Alternative for Cron

Hi... I want to know whether if there is any alternative for cron.:confused: I had written a script which checks for all system/application processes every 15 min(placed in cron though). But looks funny - what if cron daemon isn't running!! and expecting that script to update the OUTPUT FILE... (5 Replies)
Discussion started by: reddybs
5 Replies

9. Shell Programming and Scripting

cut -f (or awk alternative)

I'm trying the following as i wish to get the last field in a report containing several fields, each line can have more fields than the previous, so I tried rvwords=`echo $rvstatus |wc -w` rvstat=`echo $rvstatus |cut -f $rvwords` but it prints the whole variable contents of $rvstatus rather... (6 Replies)
Discussion started by: gefa
6 Replies

10. Shell Programming and Scripting

loop alternative?

I have a 1-column file with random numbers. this script runs to subtract 1 to the number and written to a file. With over 10,000 lines, it takes >2 minutes to complete the loop operation. is there an alternative awk/sed for looping to reduce the wait? Thanks! #!/bin/ksh for N in `cat... (2 Replies)
Discussion started by: apalex
2 Replies
Login or Register to Ask a Question