Sponsored Content
Top Forums Shell Programming and Scripting search and replace dynamic data in a shell script Post 41933 by csejl on Friday 17th of October 2003 08:21:36 PM
Old 10-17-2003
search and replace dynamic data in a shell script

Hi,

I have a file that looks something like this:

...
0,6,256,87,0,0,0,1187443420
0,6,438,37,0,0,0,1187443380
0,2,0,0,0,10,0,1197140320
0,3,0,0,0,10,0,1197140875
0,2,0,0,0,23,0,1197140332
0,3,0,0,0,23,0,1197140437
0,2,0,0,0,17,0,1197140447
0,3,0,0,0,17,0,1197140543
0,2,0,0,0,30,0,1197140237
0,3,0,0,0,30,0,1197140231
0,6,375,98,0,0,0,1187443420
0,6,222,11,0,0,0,1187443380
...

The number in the last column is dynamic. "0,2,0,0,0" is always followed by "0,3,0,0,0" What I need to do is as follows:

- I need to find the first line that starts out with 0,2,0,0,0
- Compare the column 6 to mynumber_1 (10) and if they match I move on to the next line
- Make sure in the next line the column 6 is also 10
- This time, I do the same thing with mynumber_2
- I repeat until mynumber_4 (30 in this case) is found
- Add two lines of my own data in between:

0,3,0,0,0,30,0,1197140231 and 0,6,375,98,0,0,0,1187443420

- Basically, the end result should be new file with two more lines of data in the right place
- In next iteration, I just have to get to the two new lines that I added and update the column6

I am thinking of doing the following:

- cat each line and see if it starts with 0,2,0,0,0. Is there a easy way to do this? In Perl, you can check to see if your variable CONTAINS certain data. Can you do this in shell?

- If it doesn't start out with 0,2,0,0,0 then cat $line >> new_file

- If it does, check the subsequent lines until my last number is matched up in column 6.

- If it fails during these subsequent checks, move on to the next line and start over with my_number1

Does this sound like a good algorithm? Any suggestions? I am currently stuck at checking each line to see if it starts out with 0,2,0,0,0. Thanks.
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed - dynamic search and replace

Hi all, I have a data file formatted as in the following line: Achadd 0:35 1:35 2:35 3:40 4:40 5:40 I need the minutes converted to seconds; I wrote a script, min2sec, to do so for one datapoint. I was hoping to use sed as in the following code to call this script and... (4 Replies)
Discussion started by: x-375HK-x
4 Replies

2. Shell Programming and Scripting

search and replace with data from another file

Can someone help me in solving the problem below. I have the following two files template_file ------------ ...other data.. ...other data.. FILE_NAME= ...other data.. ...other data.. list_file ---------- <file_name1> <file_name2> <file_name3> I need to produce another output... (3 Replies)
Discussion started by: paruthiveeran
3 Replies

3. Shell Programming and Scripting

Using sed to search and replace data

Hi, Kindly need your expertise in this problem. I have to search and replace data. The problem is, the data is in the same format but slightly different content. What I need is sed commands that can work for those "slightly different content". input: ... (3 Replies)
Discussion started by: Alvin123
3 Replies

4. Shell Programming and Scripting

awk/sed to search & replace data in first column

Hi All, I need help in manipulating the data in first column in a file. The sample data looks like below, Mon Jul 18 00:32:52 EDT 2011,NULL,UAT Jul 19 2011,NULL,UAT 1] All field in the file are separated by "," 2] File is having weekly data extracted from database 3] For eg.... (8 Replies)
Discussion started by: gr8_usk
8 Replies

5. Shell Programming and Scripting

Reading the data from CSV and performing search through shell script

Hello, I am working on building a script that does the below actions together in my Linux server. 1) First, have to read the list of strings mentioned in CSV and store it in the shell script 2) Second, pick one by one from the string list, and search a particular folder for files that... (2 Replies)
Discussion started by: vikrams
2 Replies

6. UNIX for Dummies Questions & Answers

Shell script for search and replace by field

Hi, I have an input file with below data and rules file to apply search and replace by each field in the input based on exact value or pattern. Could you please help me with unix script to read input file and rules file and then create the output and reject files based on the rules file. Input... (13 Replies)
Discussion started by: chandrath
13 Replies

7. Shell Programming and Scripting

Dynamic search and replace

Hi, I am trying to search and replace a value in a file. File contains data which is stated below ruby ./scripts/CreateUsage.rb Cloud_Computing_001 Cloud_Computing_001 is the 3rd column of the file. I want to replace this Cloud_Computing_001 with the runtime value. Next time i want to... (4 Replies)
Discussion started by: Chandresh Kumar
4 Replies

8. Shell Programming and Scripting

Search and replace data

FILENAME: (Orig File) lab computer11node1 { hardware ok 19:50:56:bd:03:c2; MAC-ADDRESS 10.1.1.1; } lab computer11node2 { hardware ok 19:50:56:bd:03:c3; MAC-ADDRESS 10.1.1.2; } FILENAME2: computer11node1 11:50:56:81:33:c5 computer11node2 11:50:56:81:33:c6 computer11node3 11:50:56:81:33:c7... (5 Replies)
Discussion started by: kenshinhimura
5 Replies

9. Shell Programming and Scripting

Help needed with shell script to search and replace a set of strings among the set of files

Hi, I am looking for a shell script which serves the below purpose. Please find below the algorithm for the same and any help on this would be highly appreciated. 1)set of strings need to be replaced among set of files(directory may contain different types of files) 2)It should search for... (10 Replies)
Discussion started by: Amulya
10 Replies
SHELL-QUOTE(1p) 					User Contributed Perl Documentation					   SHELL-QUOTE(1p)

NAME
shell-quote - quote arguments for safe use, unmodified in a shell command SYNOPSIS
shell-quote [switch]... arg... DESCRIPTION
shell-quote lets you pass arbitrary strings through the shell so that they won't be changed by the shell. This lets you process commands or files with embedded white space or shell globbing characters safely. Here are a few examples. EXAMPLES
ssh preserving args When running a remote command with ssh, ssh doesn't preserve the separate arguments it receives. It just joins them with spaces and passes them to "$SHELL -c". This doesn't work as intended: ssh host touch 'hi there' # fails It creates 2 files, hi and there. Instead, do this: cmd=`shell-quote touch 'hi there'` ssh host "$cmd" This gives you just 1 file, hi there. process find output It's not ordinarily possible to process an arbitrary list of files output by find with a shell script. Anything you put in $IFS to split up the output could legitimately be in a file's name. Here's how you can do it using shell-quote: eval set -- `find -type f -print0 | xargs -0 shell-quote --` debug shell scripts shell-quote is better than echo for debugging shell scripts. debug() { [ -z "$debug" ] || shell-quote "debug:" "$@" } With echo you can't tell the difference between "debug 'foo bar'" and "debug foo bar", but with shell-quote you can. save a command for later shell-quote can be used to build up a shell command to run later. Say you want the user to be able to give you switches for a command you're going to run. If you don't want the switches to be re-evaluated by the shell (which is usually a good idea, else there are things the user can't pass through), you can do something like this: user_switches= while [ $# != 0 ] do case x$1 in x--pass-through) [ $# -gt 1 ] || die "need an argument for $1" user_switches="$user_switches "`shell-quote -- "$2"` shift;; # process other switches esac shift done # later eval "shell-quote some-command $user_switches my args" OPTIONS
--debug Turn debugging on. --help Show the usage message and die. --version Show the version number and exit. AVAILABILITY
The code is licensed under the GNU GPL. Check http://www.argon.org/~roderick/ or CPAN for updated versions. AUTHOR
Roderick Schertler <roderick@argon.org> perl v5.8.4 2005-05-03 SHELL-QUOTE(1p)
All times are GMT -4. The time now is 12:16 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy