Sponsored Content
Full Discussion: Using Grep in a Shell Script
Top Forums Shell Programming and Scripting Using Grep in a Shell Script Post 66608 by zazzybob on Wednesday 16th of March 2005 07:51:37 AM
Old 03-16-2005
So each file has a consistent number of lines, right?

The following (g)awk program will do the trick. Just set the modulus to whatever you need (i.e. if the filenames appear every 5 lines change the modulus to $0 % 5). Save this as whatever.awk and make executable, then invoke with
$ ./whatever.awk input_file
Code:
#!/usr/bin/gawk -f

BEGIN { FS="\n" }

{ if ( $0 % 4 == 0 ) {
  filename = $0
} else {
  print $0 >> filename
} }


Here's a way of doing it using bash
Code:
#!/bin/bash

current_line=0
# file_length is the number of lines in each "file"
# excluding the n.txt lines....
file_length=3

while read line; do
  if [ $(( current_line % ( file_length + 1 ) )) -eq "0" ]; then
     filename="$line"
  else
     echo "$line" >> $filename
  fi
  (( current_line = current_line + 1 ))
done < input_file

Cheers
ZB
 

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
scsnapshot(1M)						  System Administration Commands					    scsnapshot(1M)

NAME
scsnapshot - retrieve configuration data about resource groups, resource types, and resources, and generate a shell script SYNOPSIS
scsnapshot [-s scriptfile] [-o imagefile] scsnapshot [-s scriptfile] oldimage newimage DESCRIPTION
Note - Beginning with the Sun Cluster 3.2 release, Sun Cluster software includes an object-oriented command set. Although Sun Cluster software still supports the original command set, Sun Cluster procedural documentation uses only the object-oriented command set. For more infor- mation about the object-oriented command set, see the Intro(1CL) man page. The scsnapshot tool retrieves information from the Cluster Configuration Repository (CCR) about configuration data that is related to resource groups, resource types, and resources. The scsnapshot tool formats the configuration data as a shell script that can be used for the following purposes: o To replicate configuration data on a cluster that has no configured resource groups, resource types, and resources o To upgrade configuration data on a cluster that has configured resource groups, resource types, and resources The scsnapshot tool retrieves configuration data only from the CCR. Other configuration data is ignored. The scsnapshot tool does not take into account the dynamic state of different resource groups, resource types, and resources. You can use this command only in the global zone. OPTIONS
The following options are supported by the scsnapshot tool. If you use an incorrect command option, the correct way to use the command option is displayed. -s scriptfile Stores the generated script in a file called scriptfile. You can use this option only in the global zone. If this option is not specified, the generated script is written to the standard output. If a file called scriptfile already exists, it is renamed as scriptfile.old, and a new file called scriptfile is created. If a file called scriptfile.old already exists, it is overwritten. -o imagefile Stores the generated image file in a file called imagefile. You can use this option only in the global zone. If this option is not specified, an image file is not generated. If a file called imagefile already exists, it is renamed as imagefile.old, and a new file called imagefile is created. If a file called imagefile.old already exists, it is overwritten. oldimage Specifies an image file that contains the old configuration data. newimage Specifies an image file that contains the new configuration data. EXTENDED DESCRIPTION
The output of the scsnapshot tool is an executable Bourne shell-based script. Before you run the script, you might need to manually change some properties to reflect the features of your host. The script compares the following characteristics of the local cluster to the cluster where the script was generated: o Machine architecture o Version of the Solaris Operating System o Version of the Sun Cluster software If the characteristics are not the same, the script writes an error and ends. A message asks whether you want to rerun the script by using the -f option. The -f option forces the script to run, despite any difference in characteristics. The script generated by the scsnapshot tool verifies that the Sun Cluster resource type exists on the local cluster. If the resource type does not exist on the local cluster, the script writes an error and ends. A message asks whether you want to install the missing resource type before you run the script again. To run a script that is generated by the scsnapshot tool, you need solaris.cluster.resource.modify RBAC authorization. For more informa- tion, see the rbac(5) man page. USAGE
This section describes how you can use the scsnapshot tool. Retrieving Configuration Data for Resource Groups, Resource Types, and Resources scsnapshot [-s scriptfile] [-o imagefile] Used without the -o option, the scsnapshot tool generates a script that creates configuration data for clusters that do not already have configured resource groups, resource types, and resources. See Example 1. Used with the -o option, the scsnapshot tool produces an image file that represents the configuration data. The image file can be used in further invocations of the scsnapshot tool to upgrade configuration data on a cluster. See Example 2. To use the scsnapshot tool to retrieve configuration data, you need solaris.cluster.resource.read role-based access control (RBAC) autho- rization. For more information, see the rbac(5) man page. To track differences between versions of configuration data, store the image files in a source control system such as SCCS. Upgrading Configuration Data for Resource Groups, Resource Types, and Resources scsnapshot [-s scriptfile] oldimage newimage The scsnapshot tool generates a shell script that can be used to upgrade the configuration data that is contained in the oldimage file with the configuration data that is contained in the newimage file. To use the scsnapshot tool to upgrade configuration data, you do not need specific RBAC authorization. EXAMPLES
Example 1 To Generate a Shell Script That Retrieves Configuration Data for Resources Groups, Resource Types, and Resources The script that is generated in this example is called scriptfile.sh. example% scsnapshot -s scriptfile.sh Example 2 To Generate a Shell Script That Retrieves Configuration Data and Stores an Image File The script that is generated in this example is called scriptfile.sh. The configuration data is stored in an image file called imagefile. example% scsnapshot -s scriptfile.sh -o imagefile Example 3 To Generate a Shell Script That Upgrades Configuration Data on One Cluster With Configuration Data From Another Cluster This example creates a script that upgrades the configuration data on cluster1 to match the configuration data on cluster2. The configura- tion data for cluster1 is in a file called imagefile1, and the configuration data for cluster2 is in a file called imagefile2. The name of a shell script is not specified, so the generated script is written to the standard output. example% scsnapshot imagefile1 imagefile2 EXIT STATUS
The following exit status codes are returned: 0 The command completed successfully. nonzero An error occurred. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWsczu | +-----------------------------+-----------------------------+ |Interface Stability |Evolving | +-----------------------------+-----------------------------+ SEE ALSO
Intro(1CL), attributes(5), clconfiguration(5CL), rbac(5), rg_properties(5), rt_properties(5), r_properties(5) Sun Cluster 3.2 22 Jun 2006 scsnapshot(1M)
All times are GMT -4. The time now is 08:43 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy