Visit The New, Modern Unix Linux Community


file feed one line per argument


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers file feed one line per argument
# 1  
file feed one line per argument

What tools can I use to accomplish this?
I'm writing a shell script to analyze an inittab file. Here's a sample file:

init:3:initdefault:
ioin::sysinit:/sbin/ioinitrc >/dev/console 2>&1
tape::sysinit:/sbin/mtinit > /dev/console 2>&1
muxi::sysinit:/sbin/dasetup </dev/console >/dev/console 2>&1 # mux i
nit
stty::sysinit:/sbin/stty 9600 clocal icanon echo opost onlcr ixon icrn
l ignpar </dev/systty
brc1::bootwait:/sbin/bcheckrc </dev/console >/dev/console 2>&1 # fsck,
etc.
link::wait:/sbin/sh -c "/sbin/rm -f /dev/syscon; \
/sbin/ln /dev/systty /dev/syscon" >/dev/consol
e 2>&1
cprt::bootwait:/sbin/cat /etc/copyright >/dev/syscon # legal
req
sqnc::wait:/sbin/rc </dev/console >/dev/console 2>&1 # syste
m init
#powf:Smilieowerwait:/sbin/powerfail >/dev/console 2>&1 # power
fail
cons:123456:respawn:/usr/sbin/getty console console # syste
m console
#ttp1:234Smilieff:/usr/sbin/getty -h tty0p1 9600 # UPS now using this port
#ttp2:234:respawn:/usr/sbin/getty -h tty0p2 9600
#ttp3:234:respawn:/usr/sbin/getty -h tty0p3 9600
#ttp4:234:respawn:/usr/sbin/getty -h tty0p4 9600
#ttp5:234:respawn:/usr/sbin/getty -h tty0p5 9600
ems1::bootwait:/sbin/rm -f /etc/opt/resmon/persistence/runlevel4_flag
ems2::bootwait:/sbin/cat </dev/null >/etc/opt/resmon/persistence/reboo
t_flag
ems3:3456:wait:/usr/bin/touch /etc/opt/resmon/persistence/runlevel4_fl
ag
ems4:3456:respawn:/etc/opt/resmon/lbin/p_client
ups::respawn:rtprio 0 /usr/lbin/ups_mond -f /etc/ups_conf


I want to read each line, and do various things, (i.e. ignore lines that begin with #)but what will send each line as an argument?
# 2  
The basic strategy woudl be to read each
line in a loop:

!#/bin/sh

while read iline
do
echo $iline
done < /etc/inittab

exit 0

In each iteration of the loop, you have the
entire line in the variable $iline.
From this point, the next step is to process
each line in the loop however you need to.
You can use awk for this task if you desire.

I just gave you the very basics as to not give
away all the answers but it's a least a starting
point Smilie
# 3  
thanks rwb, exactly what i was looking for. but i'm stuck there now. i was planning to use grep to anaylze inittab, you know, grep lines that begin with a # and ignore them, check the fields...
but how can I pass each line as the argument to grep?
I tried this way, but i'm not quite where I need to be yet:

#!/bin/ksh


while read iline
do
echo $iline|xargs
echo $*
grep ^# $*

as you can see, I"m using the Korn shell..

any more help will be greatly appreciated,

thanks again
# 4  
I would use a different approach. I would
use grep to remove all the lines that
start with "#"...

!#/bin/ksh

# create a tmp file with all lines in /etc/inittab
# that do not start with "#"
grep -v ^# /etc/inittab > /tmp/mytest.$$

#loop through each line in the tmp file
while read iline
do
# this is where you would process $iline
# using awk and/or sed is easiest IMHO
...
...
done < /tmp/mytest.$$

# clean up after yourself
rm -f /tmp/mytest.$$

exit 0

...what you do in the processing of each line
is dependent on what "and do various things"
entails. I'd also recommend a careful reading
of the grep man page. There's alot of good stuff
you can do with it. Also, the following URL
is a good online reference for AWK:
http://vectorsite.tripod.com/tsawk0.html
# 5  
I'll use sed and awk when I get more familiar with them, I think grep will work for what I need to do, here's my script so far, with some of the "various things" I want the loop to do included as comments. Your help has made a drastic improvement already, what will the next step be...

#!/bin/ksh

# Usage: chkinit (inittab file)

# This creates a temp file to work with,using all lines
# in the inittab that do not begin with # (assumes the argument is an
# inittab file) and all lines that are not blank

grep -v -e "^#" -e "^$" $1 > /tmp/mytest.$$

# this will loop through each line in the tmp file

while read iline
do
# case (can I use case here... if so how?)
# I would like to: * ensure that all lines contain 4 fields
# * make sure the id field has from 1 to 4 chars
# * make sure that the run levels are valid for
the process indicated
done < /tmp/mytest.$$

rm -f /tmp/mytest.$$
# 6  
Well... I don't believe grep by itself will
give you what you need. This is why I suggest
awk. I myself would in fact use awk for this
sort of thing. ksh itself does not provide
good string handling capabilities (at least not
without alot of excess work). For example,
the resulting lines from /etc/initab may be
somthing like...

1:2345:respawn:/sbin/mingetty tty1
2:2345:respawn:/sbin/mingetty tty2
3:2345:respawn:/sbin/mingetty tty3
...
...

...so here you can see you have 4 fields...
id:runlevels:actionSmilierocess
...separated by a ":" and you need to examine
these individually so you would need to
separate each field into distinct variables.
awk provides simple methods for doing this
very thing (and much much more). You could
perhaps use "case" against a single field
variable (once extracted from the line) to
apply rules that are defined as general for
a given value. Special rules however will
tend to create an overly complex script and
more prone to errors.

Again, my recommendation would be to learn
awk (at least the basics of it) and try some
things. After that if you have more questions,
I'm sure you can get answeres on this forum.Smilie
# 7  
I would just use ksh by itself. It can read each record, split the record into fields, count the characters in a field, etc. In short, it can do everything you need.

Also, remember that lines are not the same as records in inittab. You can end a line with a backslash to continue the record on the next line. Your grep method doesn't handle this.

Here is some sample code to show the concept...

Code:
#! /usr/bin/ksh

IFS=":"
while read id rstate action process ; do

#  ignore comments
    [[ $id = \#* ]] && continue

#  count fields
    ((nf=0))
    [[ ! -z $id      ]]  && ((nf=nf+1))
    [[ ! -z $rstate  ]]  && ((nf=nf+1))
    [[ ! -z $action  ]]  && ((nf=nf+1))
    [[ ! -z $process ]]  && ((nf=nf+1))

#  ignore all blanks
    (( ! nf )) && continue

    idlength=${#id}
    echo $id $idlength
done
exit 0

You should be able to code in any tests you want in place of that echo statement.
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #839
Difficulty: Medium
WebSocket is a computer communications protocol, providing half-duplex communication channels over a single TCP connection.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk issue splitting a fixed-width file containing line feed in data

Hi Forum. I have the following script that splits a large fixed-width file into smaller multiple fixed-width files based on input segment type. The main command in the script is: awk -v search_col_pos=$search_col_pos -v search_str_len=$search_str_len -v segment_type="$segment_type"... (8 Replies)
Discussion started by: pchang
8 Replies

2. Shell Programming and Scripting

Want to remove a line feed depending on number of tabs in a line

Hi! I have been struggling with a large file that has stray end of line characters. I am working on a Mac (Lion). I mention this only because I have been mucking around with fixing my problem using sed, and I have learned far more than I wanted to know about Unix and Mac eol characters. I... (1 Reply)
Discussion started by: user999991
1 Replies

3. Shell Programming and Scripting

Help in removing control M and Line feed in output file.

Hi All, In my output file i am getting control m character and also the line feeds at different places and with different combinations, the content of the file is supposed to be in a single line but if there is a line feed in between then from there onwards it's going into new line. I tried... (7 Replies)
Discussion started by: Bipin Kumar
7 Replies

4. Shell Programming and Scripting

Delete a specific line from the feed file

Hi All, I have came across an issue where I will grep for a primary key and then I have to delete that particular line from the feed file and then save it. The feed file is a TAB delimited one. For example: grep 539439AE9 file1 100.00000 20090119 20090119 20090521 ... (4 Replies)
Discussion started by: filter
4 Replies

5. Shell Programming and Scripting

Remove line feed from csv file column

Hi All, i have a csv file . In the 7th column i have data that has line feed in it. Requirement is to remove the line feed from the 7th column whenever it appears There are 11 columns in the file C1,C2,C3,C4,C5,C6,C7,C8,C9,C10,C11 The value in C7 contains line feed ( Alt + Enter ),... (2 Replies)
Discussion started by: r_t_1601
2 Replies

6. Shell Programming and Scripting

Remove line feed from csv file column

Hi All, My requirement is to remove line (3 Replies)
Discussion started by: r_t_1601
3 Replies

7. Shell Programming and Scripting

Get the 1st 99 characters and add new line feed at the end of the line

I have a file with varying record length in it. I need to reformat this file so that each line will have a length of 100 characters (99 characters + the line feed). AU * A01 EXPENSE 6990370000 CWF SUBC TRAVEL & MISC MY * A02 RESALE 6990788000 Y... (3 Replies)
Discussion started by: udelalv
3 Replies

8. Shell Programming and Scripting

replace last form feed with line feed

Hi I have a file with lots of line feeds and form feeds (page break). Need to replace last occurrence of form feed (created by - echo "\f" ) in the file with line feed. Please advise how can i achieve this. TIA Prvn (5 Replies)
Discussion started by: prvnrk
5 Replies

9. Shell Programming and Scripting

Unable to display correctly the contents of a file without a line feed

I am using AIX and ksh. I need to display the contents of a file that has a pid (process id). Because the file is open, it doesn't have the line feed or new line, so for some reason if I do this: `cat $pid` , where $pid is the name of the fully qualified file, it displays test3.sh: 426110:... (1 Reply)
Discussion started by: Gato
1 Replies

10. Shell Programming and Scripting

Removing Carriage Return and or line feed from a file

Hello I'm trying to write a shell script which can remove a carriage return and/or line feed from a file, so the resulting file all ends up on one line. So, I begin with a file like this text in file!<CR> line two!<CR> line three!<CR> END!<CR> And I want to end up with a file... (1 Reply)
Discussion started by: tbone231
1 Replies

Featured Tech Videos