Sponsored Content
Full Discussion: awk substr
Top Forums Shell Programming and Scripting awk substr Post 302785097 by alister on Sunday 24th of March 2013 07:14:37 PM
Old 03-24-2013
Quote:
Originally Posted by Jotne
If there are good separators to use, do use that instead of counting characters.
Scrutinizer is the way to go.
That assertion is unjustified and may fill the OP with unwarranted confidence.

To be clear, I am not suggesting that there is something inherently wrong with Scrutinizer's code. What I am saying is that we don't have enough information to unequivocally state which approach is best.

It's easiest to demonstrate by presenting a hypothetical though very plausible scenario which does not contradict anything the OP has said in this thread:

Let's assume the following conditions are true:
1) The application which generates the data (generator) defines its format as a simple, straightforward, tab-delimited file.
2) There are no constraints on the contents of any field except that they cannot contain a tab.
3) There is some information in the second field which a second application (the consumer, i.e. a suggestion in this thread) needs to extract. This information is delimited by an "=" and a ";".
4) As is typically the case, the generator has no special knowledge of the consumer.

If the consumer uses anything but a single tab as the field separator, the door is opened for spectacular failure (hopefully it's nothing mission critical).

The generator, even if expertly coded with thorough error handling and sanity checks, will not hesitate to include a character which the consumer will treat as a non-tab field separator. To the generator, none of "ch=", ";ch", or "c<spc>h" is noteworthy. To a consumer that's in sync with the generator, also using a single tab to delimit, none of these is a problem. However, to a consumer accepting any of <spc>, <tab>, ;, and = as delimiters ... ouch!

While Scrutinizer's approach is clearly insufficiently robust for use under some circumstances, it may very well be a perfectly fine solution under others (e.g. a quick one-off script or for processing data whose fields comply with additional content restrictions). To which category the OP's situation belongs, we do not know.

Regards,
Alister
This User Gave Thanks to alister For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk substr?

Sorry if this has been posted before, I searched but not sure what I really want to do. I have a file with records that show who has logged into my application: 2003-03-14:I:root: Log_mesg: registered servername:userid. (more after this) I want to pull out the userid, date and time into... (2 Replies)
Discussion started by: MizzGail
2 Replies

2. Shell Programming and Scripting

How to use awk substr ?

Hi all, I have a flatfile I would like to get ext = 7950 , how do I do that ? if ($1 == "CTI-ProgramStart") { ext = substr($9,index($9,"Extension")+11,4); But why it is not working ???? Please help . Thanks (1 Reply)
Discussion started by: sabercats
1 Replies

3. UNIX for Dummies Questions & Answers

awk or substr

i have a variable 200612 the last two digits of this variable should be between 1 and 12, it should not be greater than 12 or less than 1 (for ex: 00 or 13,14,15 is not accepted) how do i check for this conditions in a unix shell script. thanks Ram (3 Replies)
Discussion started by: ramky79
3 Replies

4. Shell Programming and Scripting

awk substr

Hi I have multiple files that name begins bidb_yyyymm. (yyyymm = current year month of file creation). What I want to do is look at the files and where yyyymm is older than 1 month I want to remove the file from the server. I was looking at looping through the files and getting the yyyymm... (2 Replies)
Discussion started by: colesga
2 Replies

5. UNIX for Dummies Questions & Answers

substr first element using awk

I have a variable '$test' that has the following string value: $test = 123|456|789|0123 How would I select just the first element ('123') using awk. Note: '|' is the delimiter, and the length of each field can vary. Thanks, - CB (3 Replies)
Discussion started by: ChicagoBlues
3 Replies

6. Shell Programming and Scripting

Help with awk and substr

I have the following to find lines matching "COMPLETE" and extract parts of it using substr. sed -n "/COMPLETE/p" 1.txt | awk 'BEGIN { FS = "\" } {printf"%s %s:%s \n", substr($3,17,3),substr($6,4,1), substr($7,4,1)}' | sort | uniq > temp.txt Worked fine until the numbers in 2nd & 3rd substr... (5 Replies)
Discussion started by: zpn
5 Replies

7. Shell Programming and Scripting

awk substr

HI I am using awk and substr function to list out the directory names in the present working directory . I am using below code ls -l | awk '{ if ((substr($1,1,1)) -eq d) {print $9 }}' But the problem is i am getting all the files and directories listed where as the requirement i wrote... (7 Replies)
Discussion started by: prabhu_kumar
7 Replies

8. Shell Programming and Scripting

Substr with awk

Hi to all, I'm here again, cause I need your help to solve another issue for me. I have some files that have this name format: date_filename.csv In my shell I must rename each file removing the date so that the file name is filename.csv To do this I use this command: fnames=`ls ${fname}|... (2 Replies)
Discussion started by: leobdj
2 Replies

9. Shell Programming and Scripting

HELP : awk substr

Hi, - In a file test.wmi Col1 | firstName | lastName 4003 | toto_titi_CT- | otot_itit - I want to have only ( colones $7,$13 and $15) with code 4003 and 4002. for colone $13 I want to have the whole name untill _CT- or _GC- 1- I used the command egrep with awk #egrep -i... (2 Replies)
Discussion started by: georg2014
2 Replies

10. Shell Programming and Scripting

awk and substr

Hello All; I have an input file 'abc.txt' with below text: 512345977,213458,100021 512345978,213454,100031 512345979,213452,100051 512345980,213455,100061 512345981,213456,100071 512345982,213456,100091 512345983,213457,100041 512345984,213451,100011 I need to paste the first field... (10 Replies)
Discussion started by: mystition
10 Replies
BUF_RING(9)						   BSD Kernel Developer's Manual					       BUF_RING(9)

NAME
buf_ring, buf_ring_alloc, buf_ring_free, buf_ring_enqueue, buf_ring_dequeue_mc, buf_ring_dequeue_sc, buf_ring_count, buf_ring_empty, buf_ring_full, buf_ring_peek, -- multi-producer, {single, multi}-consumer lock-less ring buffer SYNOPSIS
#include <sys/param.h> #include <sys/buf_ring.h> struct buf_ring * buf_ring_alloc(int count, struct malloc_type *type, int flags, struct mtx *sc_lock); void buf_ring_free(struct buf_ring *br, struct malloc_type *type); int buf_ring_enqueue(struct buf_ring *br, void *buf); void * buf_ring_dequeue_mc(struct buf_ring *br); void * buf_ring_dequeue_sc(struct buf_ring *br); int buf_ring_count(struct buf_ring *br); int buf_ring_empty(struct buf_ring *br); int buf_ring_full(struct buf_ring *br); void * buf_ring_peek(struct buf_ring *br); DESCRIPTION
The buf_ring functions provide a lock-less multi-producer and lock-less multi-consumer as well as single-consumer ring buffer. The buf_ring_alloc() function is used to allocate a buf_ring ring buffer with count slots using malloc_type type and memory flags flags. The single consumer interface is protected by sc_lock. The buf_ring_free() function is used to free a buf_ring. The user is responsible for freeing any enqueued items. The buf_ring_enqueue() function is used to enqueue a buffer to a buf_ring. The buf_ring_dequeue_mc() function is a multi-consumer safe way of dequeueing elements from a buf_ring. The buf_ring_dequeue_sc() function is a single-consumer interface to dequeue elements - requiring the user to serialize accesses with a lock. The buf_ring_count() function returns the number of elements in a buf_ring. The buf_ring_empty() function returns TRUE if the buf_ring is empty, FALSE otherwise. The buf_ring_full() function returns TRUE if no more items can be enqueued, FALSE otherwise. The buf_ring_peek() function returns a pointer to the last element in the buf_ring if the buf_ring is not empty, NULL otherwise. RETURN VALUES
The buf_ring_enqueue() function return ENOBUFS if there are no available slots in the buf_ring. HISTORY
These functions were introduced in FreeBSD 8.0. BSD
September 27, 2012 BSD
All times are GMT -4. The time now is 03:56 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy