Need help separating file lines into three classes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help separating file lines into three classes
# 1  
Old 10-27-2012
Need help separating file lines into three classes

Hi folks,

What I have are config files with lines that: are blank, start with a "!" or start with char's(or a blank space and then char's)

I am using ksh

I can display each line by doing:
Code:
for INDEX in {0..$LENGTH}
do
   echo "${data[$INDEX]}" 
done

What I need to do requires I can separate the lines into the three classes
the psudo code looks like this:
Code:
for INDEX in {0..$LENGTH}
do
 -psudoCode
  If echo ^{${data[$INDEX]}"} = "  "  # note: two blanks in quotes
    then
      If echo ^{${data[$INDEX]}"} = "!"
        then
          do something
        else
          do something else
     fi
  fi
 -end psudoCode
done


Now, I know I can do this using if and regex, but have tried many ways...and searched the web for answers to try. I can't seem to crack this and am hoping someone can help

Thanks!

Marc
# 2  
Old 10-27-2012
awk is inherently able to deal with this.
please give us sample input and expected output.

What we have so far is interesting but not on the path to helping you.
# 3  
Old 10-27-2012
Partial Input

file starts below
_______________________

!
version 12.4
service timestamps debug datetime msec
service timestamps log datetime msec
no service password-encryption
!
hostname MarcsTestbox
!
boot-start-marker
boot-end-marker
!
logging message-counter syslog
enable secret {FIELD DELETED] [FIELD DELETED]
!
no aaa new-model
clock timezone
.
.
.
__________
End of input

Expected output after first processing will take the first of actual config line after a "!" as a heading and provide a menu.
So from above, the first run through the $data[$INDEX] array would provide:

"Menu of options:

1 version
2 Hostname
3 boot
4 logging"

Then, after capturing the data for a menu, I want to be able to select a number and get the block corresponding to that heading

Ex:
if I hit "2" and enter
I get:

hostname MarcsTestbox

if I hit "4" and enter
I get:

logging message-counter syslog
enable secret {FIELD DELETED] [FIELD DELETED]


Thanks for helping!

Marc
# 4  
Old 10-28-2012
Please repost your sample input using code tags. Since you've used the circumflex character in your pseudocode, I'm assuming that you're looking for some lines starting with two space characters; but none of the lines in your sample input have any leading space characters. Smilie
Please ignore this posting. When I first read message #3 in this thread, I was trying to make sense of it given the pseudocode in message #1. I now realize that message #1 can be ignored and the full specification of what you want is in message #3.

Last edited by Don Cragun; 10-28-2012 at 07:19 AM.. Reason: No longer confused.
# 5  
Old 10-28-2012
On top,I'd be surprised if your snippet for INDEX in {0..$LENGTH} would work...
# 6  
Old 10-28-2012
Quote:
Originally Posted by Don Cragun
Please repost your sample input using code tags. Since you've used the circumflex character in your pseudocode, I'm assuming that you're looking for some lines starting with two space characters; but none of the lines in your sample input have any leading space characters. Smilie
Please ignore this posting. When I first read message #3 in this thread, I was trying to make sense of it given the pseudocode in message #1. I now realize that message #1 can be ignored and the full specification of what you want is in message #3.
Code:
!
version 12.4
service timestamps debug datetime msec
service timestamps log datetime msec
no service password-encryption
!
hostname MarcsTestbox
!
boot-start-marker
boot-end-marker
!
logging message-counter syslog
enable secret {FIELD DELETED] [FIELD DELETED]
!
no aaa new-model
clock timezone
  .
  .
  .

Expected output after first processing will take the first of actual config line after a "!" as a heading and provide a menu.
So from above, the first run through the $data[$INDEX] array would provide:

Code:
"Menu of options:

 1 version
2 Hostname
3 boot
4 logging"

Then, after capturing the data for a menu, I want to be able to select a number and get the block corresponding to that heading

Ex:
if I hit "2" and enter
I get:

hostname MarcsTestbox

if I hit "4" and enter
I get:

logging message-counter syslog
enable secret {FIELD DELETED] [FIELD DELETED]

Thanks for helping!

Marc
# 7  
Old 10-28-2012
Quote:
Originally Posted by RudiC
On top,I'd be surprised if your snippet for INDEX in {0..$LENGTH} would work...
Rudi,
In recent versions of ksh:
Code:
x=4
for i in {0..$x}
do echo $i
done

produces:
Code:
0
1
2
3
4

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help in separating a multilingual file

Hello, I have a text file running into around 100 thousand+ lines which has the following rigid structure: Each field is separated by a comma. Some examples are given below: 23,Chinttaman Pagare,चिंतमण पगारे 24, Chinttaman Pateel,चिंतामण पाटल 25, Chinttaman Rout,चिंतामण राऊत 26,... (3 Replies)
Discussion started by: gimley
3 Replies

2. Shell Programming and Scripting

How to generate a csv files by separating the values from the input file based on position?

Hi All, I need help for doing the following. I have a input file like: aaaaaaaaaabbbbbbbbbbbbbbbbbbbb cccbbbbbaaaaaadddddaaaabbbbbbb now I am trying to generate a output csv file where i will have for e.g. 0-3 chars of each line as the first column in the csv, 4-10 chars of the line as... (3 Replies)
Discussion started by: babom
3 Replies

3. Shell Programming and Scripting

AWK separating a file into an array

Is there a way to have awk put successive records into an array in a bash script? I have files that say things like name :title :salary Bob :Instructor :30,000 Joyce :Instructor :30,000 Patrick :Manager :40,000 What I want to do is seperate this file into an array so that... (8 Replies)
Discussion started by: tgidzak
8 Replies

4. Programming

Separating two classes in two files

I have a file Map.hh shown below. I want to put the two classes Phase and Map in two different files Phase.hh and Map.hh. I have forward declaration before the Map class. How can I tackle this situation? ////////////////////////////////////////////////////////////////////////// #ifndef... (3 Replies)
Discussion started by: kristinu
3 Replies

5. Shell Programming and Scripting

Need help separating a file

Hi all, I have a single text file, Contig3.fasta, that looks like this: >NAME1 ACCTGGTA >NAME2 GGTTGGACA >NAME3 ATTTTGGGCCAnd It has about 100 items like this in it. What I would like to do is copy each item into 100 different text files, and have them named a certain way Output... (4 Replies)
Discussion started by: repiv
4 Replies

6. Shell Programming and Scripting

Separating list of input files (*.file) with a comma in bash script

Hi all, I'm trying to get a bash script working for a program (bowtie) which takes a list of input files (*.fastq) and assembles them to an output file (outfile.sam). All the .fastq files are in one folder in my home directory (~/infiles). The problem is that the 'bowtie' requires that... (7 Replies)
Discussion started by: TuAd
7 Replies

7. Shell Programming and Scripting

Separating delimited file by pattern with exclusion list

I have a file with the contents below jan_t=jan;feb_t=feb;mar_t=mar;year=2010 jan_t=null;feb_t=feb;mar_t=mar;year=2010 jan_t=jan;feb_t=feb;mar_t=mar;year=2010 I want to extract out all the fields values ending with "_t" , however, i want to exclude feb_t and mar_t from the results In... (6 Replies)
Discussion started by: alienated
6 Replies

8. Programming

Use of C++ Classes

I was wondering if I could put the section at the beginning rather than at the end before the definition of the class. const REAL ModMisfit::DefMinDT = 0.01; const REAL ModMisfit::DefSigma0 = 0.01; const double ModMisfit::DefDAngSh = 2; const REAL ModMisfit::DefKBeta = 5;... (2 Replies)
Discussion started by: kristinu
2 Replies

9. Shell Programming and Scripting

Merging files into a single tab delimited file with a space separating

I have a folder that contains say 50 files in a sequential order: cdf_1.txt cdf_2.txt cdf_3.txt cdf_3.txt . . . cdf_50.txt. I need to merge these files in the same order into a single tab delimited file. I used the following shell script: for x in {1..50}; do cat cdf_${x}.txt >>... (3 Replies)
Discussion started by: Lucky Ali
3 Replies

10. Shell Programming and Scripting

Separating values from a file and putting to a variable

I am writing into a file testfile.txt values like ./XXXXXXCZ1/tprcm10c.bin ./XXXXXXCZ1_HOT/tprcm09c.bin ./XXXXXXCZ_cold/tprcm05c.bin I want to store the values of tprcm*.bin and XXXXXXCZ* in separate variables Can anybody Pls hlp me out with this ... Thanks (2 Replies)
Discussion started by: ultimatix
2 Replies
Login or Register to Ask a Question