Sponsored Content
Full Discussion: Using Grep in a Shell Script
Top Forums Shell Programming and Scripting Using Grep in a Shell Script Post 66592 by nbvcxzdz on Wednesday 16th of March 2005 05:26:29 AM
Old 03-16-2005
Using Grep in a Shell Script

Hi everyone,

Im trying to write a Shell script that basically creates a set of files based on a file with many records. For example if a file called dummy has the following content:

a.txt
1st line of a's text file
2nd line of a's text file
3rd line of a's text file
b.txt
1st line of b's text file
2nd line of b's text file
3rd line of b's text file

After running my shell script, 2 files should be created. The first should be called a.txt and its contents should be:
1st line of a's text file
2nd line of a's text file
3rd line of a's text file

The second file created should be called b.txt and its contents should be:
1st line of b's text file
2nd line of b's text file
3rd line of b's text file

So essentialy the name of the file is followed by its content in dummy. I've attempted to write a script to do this but ive being having problems getting it to work. If someone has done this before using similar code or completely different code could you please help me out? I think i have some error in my grep line and also other minor errors. My shell script code
is as follows:

#look at one line at a time in dummy file
line_number=1
#number of lines in file, temporarliy set to 9 but should be whatever the #number of lines in the file is
LENGTH=9

while [ $line_number -le $LENGTH ]
do
$current_line=`tail +$line_number < dummy|head -n$line_number|cat`

if [ grep ".txt" $current_line ]
then
FILENAME=$current_line
else
`echo $current_line >> $FILENAME`
fi

#$line_number='exp $line_number + 1'
line_number=`expr $line_number+1`
done




thanks
nbvcxzdz
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

grep, sed in a shell script

Hi, I have a problem with a simple script I am trying to write. I want a user to type grep, sed commands that are then stored in variables. Those variables are stored in a function, and the function is then called to execute the commands. The idea is that the user does it step by step. ... (4 Replies)
Discussion started by: Trufla
4 Replies

2. Shell Programming and Scripting

grep in Shell script

Hello I do want to write a script which will check any errors say "-error" in the log file then have to send email to the concern person . And the concern person will correct the error . Next time if the script runs eventhough the error has been corrected it will ... (1 Reply)
Discussion started by: Krishnaramjis
1 Replies

3. Shell Programming and Scripting

Grep or Tail in shell script

Hi, I am writing a shell script that checks catalina logs on a production system and mails me if it detects errors. It greps the logs for known errors which i have defined as variables. The problem is the logs are huge, approx 30,000 before they rotate. So I am forced to use grep instead... (3 Replies)
Discussion started by: Moxy
3 Replies

4. Shell Programming and Scripting

Shell script grep help

Hey there, newbie question : echo "::kmastat" | /usr/bin/mdb -k | grep Total | grep "kmem_*" Total 17326080 432853 0 Total 426508288 65458 0 Total 704757760 1572001732 0 Total ... (11 Replies)
Discussion started by: shriyer
11 Replies

5. Shell Programming and Scripting

Grep command in shell script

Hi, I have written the following shell script - Error_String="error" var1="| grep -v note1 | grep -v note2" grep -i $Error_String /users/mqm/Pwork/Err/*.out $var1 The above script gives error saying "grep: can't open | grep: can't open grep grep: can't open -v" etc In my program... (3 Replies)
Discussion started by: prasannasupp
3 Replies

6. Shell Programming and Scripting

Simple Shell Script to Grep

Hi guys, I have written this script, however the outcome is invalid. It contains grep search that is not needed: Script: #!/bin/bash #this is a test script FILES=$(ls /home/student/bin/dir1/*) GREPFUNC=$(grep -E -i "login|Successfully" ORProxyTC`date '+%m%d%Y'`*.txt/ ${FILES})... (14 Replies)
Discussion started by: DallasT
14 Replies

7. Shell Programming and Scripting

How to grep sql error in shell script and exit the script?

I need help in the following script. I want to grep the sql errors insert into the error table and exit the shell script if there is any error, otherwise keep running the scripts. Here is my script #!/bin/csh -f source .orapass set user = $USER set pass = $PASS cd /opt/data/scripts echo... (2 Replies)
Discussion started by: allinshell99
2 Replies

8. Shell Programming and Scripting

Shell Script with Grep

Hi guys - below is my script that is checking for current file, size and timestamp. However I added a "grep" feature in it (line in red), but not getting the desired result. I am trying to acheive in output: 1. Show me the file name, timestamp, size and grep'ed words It would be a... (2 Replies)
Discussion started by: DallasT
2 Replies

9. Shell Programming and Scripting

Grep in Shell script

hi guys very new to this game so excuse my ignorance. I need to create a script that simply greps for a text string and then outputs a message depending on whether the text string is there or not. The script I have setup is below, but whenever I run it I get the following error: ... (5 Replies)
Discussion started by: ap2112
5 Replies

10. Shell Programming and Scripting

Speeding up shell script with grep

HI Guys hoping some one can help I have two files on both containing uk phone numbers master is a file which has been collated over a few years ad currently contains around 4 million numbers new is a file which also contains 4 million number i need to split new nto two separate files... (4 Replies)
Discussion started by: dunryc
4 Replies
Pod::InputObjects(3pm)					 Perl Programmers Reference Guide				    Pod::InputObjects(3pm)

NAME
Pod::InputObjects - objects representing POD input paragraphs, commands, etc. SYNOPSIS
use Pod::InputObjects; REQUIRES
perl5.004, Carp EXPORTS
Nothing. DESCRIPTION
This module defines some basic input objects used by Pod::Parser when reading and parsing POD text from an input source. The following objects are defined: package Pod::Paragraph An object corresponding to a paragraph of POD input text. It may be a plain paragraph, a verbatim paragraph, or a command paragraph (see perlpod). package Pod::InteriorSequence An object corresponding to an interior sequence command from the POD input text (see perlpod). package Pod::ParseTree An object corresponding to a tree of parsed POD text. Each "node" in a parse-tree (or ptree) is either a text-string or a reference to a Pod::InteriorSequence object. The nodes appear in the parse-tree in the order in which they were parsed from left-to-right. Each of these input objects are described in further detail in the sections which follow. Pod::Paragraph An object representing a paragraph of POD input text. It has the following methods/attributes: Pod::Paragraph->new() my $pod_para1 = Pod::Paragraph->new(-text => $text); my $pod_para2 = Pod::Paragraph->new(-name => $cmd, -text => $text); my $pod_para3 = new Pod::Paragraph(-text => $text); my $pod_para4 = new Pod::Paragraph(-name => $cmd, -text => $text); my $pod_para5 = Pod::Paragraph->new(-name => $cmd, -text => $text, -file => $filename, -line => $line_number); This is a class method that constructs a "Pod::Paragraph" object and returns a reference to the new paragraph object. It may be given one or two keyword arguments. The "-text" keyword indicates the corresponding text of the POD paragraph. The "-name" keyword indicates the name of the corresponding POD command, such as "head1" or "item" (it should not contain the "=" prefix); this is needed only if the POD para- graph corresponds to a command paragraph. The "-file" and "-line" keywords indicate the filename and line number corresponding to the beginning of the paragraph $pod_para->cmd_name() my $para_cmd = $pod_para->cmd_name(); If this paragraph is a command paragraph, then this method will return the name of the command (without any leading "=" prefix). $pod_para->text() my $para_text = $pod_para->text(); This method will return the corresponding text of the paragraph. $pod_para->raw_text() my $raw_pod_para = $pod_para->raw_text(); This method will return the raw text of the POD paragraph, exactly as it appeared in the input. $pod_para->cmd_prefix() my $prefix = $pod_para->cmd_prefix(); If this paragraph is a command paragraph, then this method will return the prefix used to denote the command (which should be the string "=" or "=="). $pod_para->cmd_separator() my $separator = $pod_para->cmd_separator(); If this paragraph is a command paragraph, then this method will return the text used to separate the command name from the rest of the paragraph (if any). $pod_para->parse_tree() my $ptree = $pod_parser->parse_text( $pod_para->text() ); $pod_para->parse_tree( $ptree ); $ptree = $pod_para->parse_tree(); This method will get/set the corresponding parse-tree of the paragraph's text. $pod_para->file_line() my ($filename, $line_number) = $pod_para->file_line(); my $position = $pod_para->file_line(); Returns the current filename and line number for the paragraph object. If called in a list context, it returns a list of two elements: first the filename, then the line number. If called in a scalar context, it returns a string containing the filename, followed by a colon (':'), followed by the line number. Pod::InteriorSequence An object representing a POD interior sequence command. It has the following methods/attributes: Pod::InteriorSequence->new() my $pod_seq1 = Pod::InteriorSequence->new(-name => $cmd -ldelim => $delimiter); my $pod_seq2 = new Pod::InteriorSequence(-name => $cmd, -ldelim => $delimiter); my $pod_seq3 = new Pod::InteriorSequence(-name => $cmd, -ldelim => $delimiter, -file => $filename, -line => $line_number); my $pod_seq4 = new Pod::InteriorSequence(-name => $cmd, $ptree); my $pod_seq5 = new Pod::InteriorSequence($cmd, $ptree); This is a class method that constructs a "Pod::InteriorSequence" object and returns a reference to the new interior sequence object. It should be given two keyword arguments. The "-ldelim" keyword indicates the corresponding left-delimiter of the interior sequence (e.g. '<'). The "-name" keyword indicates the name of the corresponding interior sequence command, such as "I" or "B" or "C". The "-file" and "-line" keywords indicate the filename and line number corresponding to the beginning of the interior sequence. If the $ptree argument is given, it must be the last argument, and it must be either string, or else an array-ref suitable for passing to Pod::ParseTree::new (or it may be a reference to a Pod::ParseTree object). $pod_seq->cmd_name() my $seq_cmd = $pod_seq->cmd_name(); The name of the interior sequence command. $pod_seq->prepend() $pod_seq->prepend($text); $pod_seq1->prepend($pod_seq2); Prepends the given string or parse-tree or sequence object to the parse-tree of this interior sequence. $pod_seq->append() $pod_seq->append($text); $pod_seq1->append($pod_seq2); Appends the given string or parse-tree or sequence object to the parse-tree of this interior sequence. $pod_seq->nested() $outer_seq = $pod_seq->nested || print "not nested"; If this interior sequence is nested inside of another interior sequence, then the outer/parent sequence that contains it is returned. Oth- erwise "undef" is returned. $pod_seq->raw_text() my $seq_raw_text = $pod_seq->raw_text(); This method will return the raw text of the POD interior sequence, exactly as it appeared in the input. $pod_seq->left_delimiter() my $ldelim = $pod_seq->left_delimiter(); The leftmost delimiter beginning the argument text to the interior sequence (should be "<"). $pod_seq->right_delimiter() The rightmost delimiter beginning the argument text to the interior sequence (should be ">"). $pod_seq->parse_tree() my $ptree = $pod_parser->parse_text($paragraph_text); $pod_seq->parse_tree( $ptree ); $ptree = $pod_seq->parse_tree(); This method will get/set the corresponding parse-tree of the interior sequence's text. $pod_seq->file_line() my ($filename, $line_number) = $pod_seq->file_line(); my $position = $pod_seq->file_line(); Returns the current filename and line number for the interior sequence object. If called in a list context, it returns a list of two ele- ments: first the filename, then the line number. If called in a scalar context, it returns a string containing the filename, followed by a colon (':'), followed by the line number. Pod::InteriorSequence::DESTROY() This method performs any necessary cleanup for the interior-sequence. If you override this method then it is imperative that you invoke the parent method from within your own method, otherwise interior-sequence storage will not be reclaimed upon destruction! Pod::ParseTree This object corresponds to a tree of parsed POD text. As POD text is scanned from left to right, it is parsed into an ordered list of text- strings and Pod::InteriorSequence objects (in order of appearance). A Pod::ParseTree object corresponds to this list of strings and sequences. Each interior sequence in the parse-tree may itself contain a parse-tree (since interior sequences may be nested). Pod::ParseTree->new() my $ptree1 = Pod::ParseTree->new; my $ptree2 = new Pod::ParseTree; my $ptree4 = Pod::ParseTree->new($array_ref); my $ptree3 = new Pod::ParseTree($array_ref); This is a class method that constructs a "Pod::Parse_tree" object and returns a reference to the new parse-tree. If a single-argument is given, it must be a reference to an array, and is used to initialize the root (top) of the parse tree. $ptree->top() my $top_node = $ptree->top(); $ptree->top( $top_node ); $ptree->top( @children ); This method gets/sets the top node of the parse-tree. If no arguments are given, it returns the topmost node in the tree (the root), which is also a Pod::ParseTree. If it is given a single argument that is a reference, then the reference is assumed to a parse-tree and becomes the new top node. Otherwise, if arguments are given, they are treated as the new list of children for the top node. $ptree->children() This method gets/sets the children of the top node in the parse-tree. If no arguments are given, it returns the list (array) of children (each of which should be either a string or a Pod::InteriorSequence. Otherwise, if arguments are given, they are treated as the new list of children for the top node. $ptree->prepend() This method prepends the given text or parse-tree to the current parse-tree. If the first item on the parse-tree is text and the argument is also text, then the text is prepended to the first item (not added as a separate string). Otherwise the argument is added as a new string or parse-tree before the current one. $ptree->append() This method appends the given text or parse-tree to the current parse-tree. If the last item on the parse-tree is text and the argument is also text, then the text is appended to the last item (not added as a separate string). Otherwise the argument is added as a new string or parse-tree after the current one. $ptree->raw_text() my $ptree_raw_text = $ptree->raw_text(); This method will return the raw text of the POD parse-tree exactly as it appeared in the input. Pod::ParseTree::DESTROY() This method performs any necessary cleanup for the parse-tree. If you override this method then it is imperative that you invoke the par- ent method from within your own method, otherwise parse-tree storage will not be reclaimed upon destruction! SEE ALSO
See Pod::Parser, Pod::Select AUTHOR
Brad Appleton <bradapp@enteract.com> perl v5.8.0 2002-06-01 Pod::InputObjects(3pm)
All times are GMT -4. The time now is 07:45 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy