Grab fields without awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grab fields without awk
# 1  
Old 07-15-2016
Grab fields without awk

so i want to avoid external calls in my script, so im trying to do the following:


Code:
AllMyLogs="hello---nallo---wello---bollo
tello---zello---jello---kello"

OLDIFS="$IFS\\\n"
IFS="---"
set -- ${AllMyLogs}
IFS="$OLDIFS"

I want it to work in a way that, when i type this:

Code:
echo $2

I get:

Code:
nallo
zello

basically, id like to pick the field/column that i want to grab, without having to use awk. Any ideas??

I want this to work on any unix os, so the solution would have to be portable.
# 2  
Old 07-15-2016
That isn't the way IFS works. Every character in $IFS is treated as a field separator; it is not an ERE as in FS in awk.

And, that isn't the way set -- works. There is only one set of positional parameters, not one set for each <newline> character encountered in the arguments passed to set.

But (assuming that the text between your --- field separators does not include any - characters), you could try something like:
Code:
printf '%s\n' "hello---nallo---wello---bollo
tello---zello---jello---kello" |
while IFS='-' read -r junk junk junk data junk
do	printf '%s\n' "$data"
done


Last edited by Don Cragun; 07-16-2016 at 06:37 AM.. Reason: Add missing closing parenthesis.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to grab data in range then search for pattern

im using the following code to grab data, but after the data in the range im specifying has been grabbed, i want to count how many instances of a particular pattern is found? awk 'BEGIN{count=0} /parmlib.*RSP/,/seqfiles.*SSD/ {print; count++ } /103 error in ata file/ END { print count }'... (3 Replies)
Discussion started by: SkySmart
3 Replies

2. Shell Programming and Scripting

awk sort based on difference of fields and print all fields

Hi I have a file as below <field1> <field2> <field3> ... <field_num1> <field_num2> Trying to sort based on difference of <field_num1> and <field_num2> in desceding order and print all fields. I tried this and it doesn't sort on the difference field .. Appreciate your help. cat... (9 Replies)
Discussion started by: newstart
9 Replies

3. Shell Programming and Scripting

awk - compare 1st 15 fields of record with 20 fields

I'm trying to compare 2 files for differences in a selct number of fields. When differnces are found it will write the whole record of the second file including appending '|C' out to a delta file. Each record will have 20 fields, but only want to do comparison of 1st 15 fields. The 1st field of... (7 Replies)
Discussion started by: sljnk
7 Replies

4. Shell Programming and Scripting

How to print 1st field and last 2 fields together and the rest of the fields after it using awk?

Hi experts, I need to print the first field first then last two fields should come next and then i need to print rest of the fields. Input : a1,abc,jsd,fhf,fkk,b1,b2 a2,acb,dfg,ghj,b3,c4 a3,djf,wdjg,fkg,dff,ggk,d4,d5 Expected output: a1,b1,b2,abc,jsd,fhf,fkk... (6 Replies)
Discussion started by: 100bees
6 Replies

5. Shell Programming and Scripting

Grab nth occurence in between two patterns using awk or sed

Hi , I have an issue where I want to parse through the output from a file and I want to grab the nth occurrence of text in between two patterns preferably using awk or sed ! TICKET NBR : 1 !GSI : 102 ! 3100.2.112.1 11/06/2013 15:56:29 ! 3100.2.22.3 98 ! 3100.2.134.2... (8 Replies)
Discussion started by: OTNA
8 Replies

6. Shell Programming and Scripting

Join fields comparing 4 fields using awk

Hi All, I am looking for an awk script to do the following Join the fields together only if the first 4 fields are same. Can it be done with join function in awk?? a,b,c,d,8,,, a,b,c,d,,7,, a,b,c,d,,,9, a,b,p,e,8,,, a.b,p,e,,9,, a,b,p,z,,,,9 a,b,p,z,,8,, desired output: ... (1 Reply)
Discussion started by: aksijain
1 Replies

7. Shell Programming and Scripting

awk to grab the most found lines

awk 'NR>=5034&&/Max/{++c}c==3{o=$0 RS $0 RS $0; print o; c=0}' logfile i would like to modify the above awk command to instead show lines that are the most frequent: for instance, given a log file containing: Warning MaxClient is having issues - please MaxClients java error...yams... (4 Replies)
Discussion started by: SkySmart
4 Replies

8. UNIX for Dummies Questions & Answers

Grab Portion of Output Text (sed, grep, awk?)

Alright, here's the deal. I'm running the following ruby script (output follows): >> /Users/name/bin/acweather.rb -z 54321 -o /Users/name/bin -c Clouds AND Sun 57/33 - Mostly sunny and cool I want to just grab the "57/33" portion, but that's it. I don't want any other portion of the line. I... (5 Replies)
Discussion started by: compulsiveguile
5 Replies

9. Shell Programming and Scripting

awk sed cut? to rearrange random number of fields into 3 fields

I'm working on formatting some attendance data to meet a vendors requirements to upload to their system. With some help on the forums here, I have the data close. But they've since changed what they want. The vendor wants me to submit three fields to them. Field 1 is the studentid field,... (4 Replies)
Discussion started by: axo959
4 Replies

10. Shell Programming and Scripting

grab the line using awk

suppose u have a file AAAAAKKSKSKMSKMSKBNNSHNKSNJNMSYNMSBH This is exactly wht the input is like Question is i want to list wht is on the line 5 tht will be A but Remember u want to extract in between say from 100 to 300 i tried using awk 'BEGIN {FS=""} {print$100,$300}' file but it will... (1 Reply)
Discussion started by: cdfd123
1 Replies
Login or Register to Ask a Question