Visit Our UNIX and Linux User Community


Korn Shell Array maximum value less than other value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Korn Shell Array maximum value less than other value
# 8  
Old 06-08-2011
Code:
#!/bin/ksh93

while read line
do
   if [[ $line =~ 'LOCATION' ]]; then
     location=$line
   fi
   if [[ $line =~ 'unknown' ]]; then
      echo "$location,$line"
   fi
done < file

This User Gave Thanks to fpmurphy For This Post:
# 9  
Old 06-08-2011
Thanks everyone for your responses. ctsgnb, your code almost worked perfectly, and it is my fault for not posting the exact file as it is. Your code printed the line after the line that I need, this is because there is a heading above the lines which I failed to include - Sorry! I tried tweaking the code a bit but I do not know nawk well enough to understand exactly what is going on there. Any insight would be appreciated. Thanks again for your help and sorry that my initial input file was not correct:

Date
Report

LOCATION: M1

Headings
line
line
line
19,8300,unknown,
line
line

LOCATION: B

Headings
line
13,eeee,unknown,eooeo
line
line

LOCATION: CL 1-2

Headings
line
line
line
line

LOCATION: CL 1-1

Heading
line
line
line

LOCATION: CL 2-2

Heading
line
line
line

LOCATION: CL 2-1

Heading
line
5,TICKLISH,unknown,83838388383
line
line
# 10  
Old 06-08-2011
the code
Code:
nawk '/^LOCATION:/{x=$0}x&&/unknown/{print x,$0;x=z}' infile

should still work ... or do you expect another output ???
This User Gave Thanks to ctsgnb For This Post:
# 11  
Old 06-08-2011
Beautiful! Thanks...I hadn't tried that one

---------- Post updated at 03:14 PM ---------- Previous update was at 11:33 AM ----------

Actually, one more quick question, I am trying to import these items to a .csv file which I am doing with a redirection >> .csv
However, I wanted to have a comma between the end of the LOCATION and the first integer in the line with 'unknown'...see below:
LOCATION: M1,19,8300,unknown,
LOCATION: B,13,eeee,unknown,eooeo
LOCATION: CL 2-1,5,TICKLISH,unknown,83838388383

This way I can easily open it in excel and have everything in seperate columns. The problem I am running into is that some of the locations have more fields than others so depending on what I use as the delimiter it seperates the locations.
# 12  
Old 06-08-2011
Then just enclose the coma in double quote :

Code:
nawk '/^LOCATION:/{x=$0}x&&/unknown/{print x","$0;x=z}' infile

This User Gave Thanks to ctsgnb For This Post:
# 13  
Old 06-08-2011
Oh okay...thanks!!

Previous Thread | Next Thread
Test Your Knowledge in Computers #697
Difficulty: Medium
The Bazaar software development model is when code is developed over the Internet open and in the public view.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to append to array within conditional block in ksh/korn shell?

Hi, I have one array created and some values are there in ksh. I want to append some other values to it based on some condition in if statement. #!/bin/ksh echo "---------------------------------------------------" set -A ipaddr_arr $(egrep -v '^#|^::|^$' /etc/hosts |awk '{print $1}'... (2 Replies)
Discussion started by: sanzee007
2 Replies

2. Shell Programming and Scripting

'*' vs. '@' in Korn Shell Array Variables

In order to use the shellcurses functions described at: Shell Curses function library I am learning about ksh, which has arrays. My trusty Kochan & Wood book says that for any Korn Shell array AR : ${AR } expands to all the defined array elements, and ${#AR } expands to the number... (3 Replies)
Discussion started by: Clovis_Sangrail
3 Replies

3. Shell Programming and Scripting

Generate array name at run time Korn shell

Hi, I am trying to define number of array based on constant derived during execution phase of a script. Here is what i am trying.. #First Part, Get LUN input from User lun_count=4 count=0 set -A my_lun while : do while ]; do read L?"Enter Lun "$count" Number:" ... (2 Replies)
Discussion started by: harris2107
2 Replies

4. Shell Programming and Scripting

need help with korn shell array

I have a korn shell script that reads a file with just one column in the file. If the file has more than 5 entries it is split using split -5. This means that is we have 15 entries I will end up with 3 files with 5 entries/lines in each and if I have 23 entries I will end up with 5 files with the... (2 Replies)
Discussion started by: kieranfoley
2 Replies

5. Shell Programming and Scripting

Could someone give me an example of awk accessing array defined in Korn Shell?

As per title and much apprecieated! (2 Replies)
Discussion started by: biglau
2 Replies

6. Shell Programming and Scripting

I can't decalar an array in my korn shell script

I have a script that contains #!/usr/bin/ksh set -A X 'hallo' 'world' echo ${X} echo ${X} when I execute it I get an errror message sh ./test.ksh ./test.ksh: -A: bad option(s) but if I do this at the command prompt I am able to create it set -A myarray '1' '2' '3' echo ${myarray}... (4 Replies)
Discussion started by: zilla30066
4 Replies

7. Shell Programming and Scripting

Unix Korn Shell Array Issue (SunOS)

Hello, I'm currently messing around with arrays for the first time in scripting (Unix Korn Shell). All I'm trying to do right now before I make things complicated is read through and print out to screen whether the read file is or is not a directory. Here is my directory: ls -l total... (5 Replies)
Discussion started by: Janus
5 Replies

8. UNIX for Dummies Questions & Answers

Maximum Command Length for Korn shell

What is the maximum size of a command which can be given in telnet command prompt in unixfor Korn shell? (2 Replies)
Discussion started by: miltony
2 Replies

9. Shell Programming and Scripting

korn shell array?

I read it is possible to provide values for an array with the -A option to the read statement; however, I have not been able to get this to work. When I execute a script with the -A option to the read statement, the shell complains that it is an illegal option. If this works, can someone provide... (5 Replies)
Discussion started by: cstovall
5 Replies

10. UNIX for Advanced & Expert Users

Oracle To Korn Shell Array

I'm attempting to populate an array in ksh using the following command: set -A $(SELECT_INVOICE | sed '/^$/d') SELECT_INVOICE is a function that executes the SQL query. Problem: Some of the invoice numbers have alpha characters with spaces(example: OVEN MICRO). The Korn shell is treating... (1 Reply)
Discussion started by: kdst
1 Replies

Featured Tech Videos