searching and editing file contents


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting searching and editing file contents
# 8  
Old 02-06-2008
Quote:
Originally Posted by mirusnet
Краткость сестра таланта.
Pls read the Rules of these forums : Rule 9
And btw: Краткость - сестра таланта
# 9  
Old 02-06-2008
Hi jayana , try this one:
Code:
> ID=4571                                                                              
>  awk '/end/{f=0;next}/id:'"${ID}"'/{f=1}f' file

...ummm , actually this is the good one:

Code:
> ID=4571
> awk '/Start/{i=0}/id:'"${ID}"'/{f=1}{a[i]=$0;i++}/end/&&f{exit}END{for (j=1;j<i-1;++j)print a[j]}' file


Last edited by Klashxx; 02-06-2008 at 09:36 AM.. Reason: Misunderstood the question
# 10  
Old 02-06-2008
dear jayana,

please try the following one ,

awk -F":" '
/^Start:/ {
fn=0
}
/^id:/ {
close(fn)
fn = $2 ".txt"
$0 = prev RS $0
}
fn {
print > fn
}
{
prev = $0
}' FileName
# 11  
Old 02-06-2008
try this

idval=$1
filename=$2
x=`grep -n "$idval" $filename | cut -d ":" -f1`
echo $x
start=`echo ${x}-2 | bc`
end=`echo ${x}+1 | bc`
echo $st
echo $to
sed -ne "${start},${end}p" $filename
# 12  
Old 02-06-2008
Hi friends, thanks for all your responses, but I have a concern here that the end need not always be after 1 line of the ID. we may have more information after the ID, but END just marks the end of the block of information.
Once I get the line number for ID (say line 35) using the grep -n, is there a way to search for the first instance of END(line 40) after it? I can get start as 2 lines behind ID (line 33) .
Once I have line 33 & 40, can I cut the lines in between and get the 8 lines into a new file?
Awk seems a little complex to understand, hence trying to explore more on grep solutions. The copy paste of awk command at shell prompt:awk '/Start/{i=0}/id:'"${ID}"'/{f=1}{a[i]=$0;i++}/end/&&f{exit}END{for (j=1;j<i-1;++j)print a[j]}' file
gave me syntax error at line 1, i could not debug the same.

Thanks for all your support

Last edited by jayana; 02-06-2008 at 07:12 PM..
# 13  
Old 02-06-2008
Try this:
Code:
> ID=4571
>  awk '/Start/{i=0}/^id:'"${ID}"'$/{f=1}{a[i]=$0;i++}/end/&&f{exit}END{if (f)for (j=1;j<i-1;++j)print a[j]}' file


Last edited by Klashxx; 02-06-2008 at 07:45 PM.. Reason: Correct a minor bug
# 14  
Old 02-06-2008
Hi.

If you are going to do this a lot, it may make sense to put some work into it. This kind of task is well-suited to cgrep, context-grep, a utility available from Bell-Labs. Here is an example using some of your data:
Code:
#!/usr/bin/env sh

# @(#) s1       Demonstrate cgrep.

#  ____
# /
# |   Infrastructure BEGIN

echo
set -o nounset

debug=":"
debug="echo"

## The shebang using "env" line is designed for portability. For
#  higher security, use:
#
#  #!/bin/sh -

## Use local command version for the commands in this demonstration.

set +o nounset
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version =o $(_eat $0 $1) cgrep
set -o nounset

echo

FILE=${1-data1}
echo " Input file $FILE:"
cat $FILE

# |   Infrastructure END
# \
#  ---

echo
echo " Results from processing:"
cgrep -I2 -w "Start:" +I2 +w "end" "id:4571" $FILE

echo
echo " Results from processing:"
cgrep -I2 -w "Start:" +I2 +w "end" "id:4600" $FILE

echo
echo " Results from processing:"
cgrep -I2 -w "Start:" +I2 +w "end" "id:4700" $FILE

exit 0

Producing:
Code:
% ./s1

(Versions displayed with local utility "version")
Linux 2.6.11-x1
GNU bash, version 2.05b.0(1)-release (i386-pc-linux-gnu)
cgrep (local) - no version provided.

 Input file data1:
Start:
name:1111
date:222
id:3333
address:12444
end
Start:
name:5555
date:3312
id:6666
address:qwds
end
Start:
name:7777
date:9090
id:4571
address:abc444
end
Start:
id:4600
end
Start:
stuff1
stuff2
stuff3
id:4700
stuff4
stuff5
stuff6
stuff7
stuff8
end

 Results from processing:
========================================
name:7777
date:9090
id:4571
address:abc444

 Results from processing:
========================================
id:4600

 Results from processing:
========================================
stuff1
stuff2
stuff3
id:4700
stuff4
stuff5
stuff6
stuff7
stuff8

The "+-w" indicate the windowing patterns, the "+-I2" cause omission of the window bracket lines.

You need to get and compile the program. It comes with a man page. The web page is cgrep home page .

It is a very useful (but non-standard) member of the grep family ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Searching the pattern and accordingly changing the contents using shell

HI, I have File1 which contains :- admins = anand,satheesha,user1,user2,user3,user4,user5,user10,vishal nonadmins = read-only @admins = rw @nonadmins = r One shell script, using that I want to change the File1 as per user input (let's say $1) which have value as 'John', so now I want to... (6 Replies)
Discussion started by: Vishal Mishra
6 Replies

2. Shell Programming and Scripting

Folder contents getting appended as strings while redirecting file contents to a variable

Hi one of the output of the command is as below # sed -n "/CCM-ResourceHealthCheck:/,/---------/{/CCM-ResourceHealthCheck:/d;/---------/d;p;}" Automation.OutputZ$zoneCounter | sed 's/$/<br>/' Resource List : <br> *************************** 1. row ***************************<br> ... (2 Replies)
Discussion started by: vivek d r
2 Replies

3. Shell Programming and Scripting

Problem with the awk in searching the contents in a file

Hello all, I am a newbie in awk. I am struggling in this problem for a long.Actually I have two files, filea and fileb. File a is actually a search key through it I have to find the corresponding japanese tag from file b. filea contains the data like this: sm982882 sm1893548 sm2420025... (3 Replies)
Discussion started by: csim_mohan
3 Replies

4. Shell Programming and Scripting

Replace partial contents of file with contents read from other file

Hi, I am facing issue while reading data from a file in UNIX. my requirement is to compare two files and for the text pattern matching in the 1st file, replace the contents in second file by the contents of first file from start to the end and write the contents to thrid file. i am able to... (2 Replies)
Discussion started by: seeki
2 Replies

5. Shell Programming and Scripting

searching a file with a specified text without using conventional file searching commands

without using conventional file searching commands like find etc, is it possible to locate a file if i just know that the file that i'm searching for contains a particular text like "Hello world" or something? (5 Replies)
Discussion started by: arindamlive
5 Replies

6. Shell Programming and Scripting

Help in searching a particular string in a file name (not inside the file contents)

Dear Unix Gurus, I am new to shell scripting and in the process of learing. I am trying to find whether a file name has today's date in MMDDYYYY format. I am using the following code and it doesn't seem like working. #!/usr/bin/ksh today=$(date '+%m%d%Y') echo today: $today file=`find... (4 Replies)
Discussion started by: shankar1dada
4 Replies

7. Shell Programming and Scripting

Problem with searching and then editing a file through shell.

Hi, I have searched through this forum as there are many similar entries but could'nt get one of them to work, either that or they were just different to what I needed. Basically I have a file, recordsDatabase. In this file are a few different fields. There is a unique identifier eg 001... (5 Replies)
Discussion started by: U_C_Dispatj
5 Replies

8. UNIX for Dummies Questions & Answers

Help with searching for a file in a directory and copying the contents of that file in a new file

Hi guys, I am a newbie here :wall: I need a script that can search for a file in a directory and copy the contents of that file in a new file. Please help me. :confused: Thanks in advance~ (6 Replies)
Discussion started by: zel2zel
6 Replies

9. UNIX for Dummies Questions & Answers

compare 2 file contents , if same delete 2nd file contents

Give shell script....which takes two file names as input and compares the contents, is both are same delete second file's contents..... I try with "diff"...... but confusion how to use "diff" with if ---else Thanking you (5 Replies)
Discussion started by: krishnampkkm
5 Replies

10. UNIX for Advanced & Expert Users

Searching contents of a file

Is there a way a command or a combination through which i can check the contents of a all files in a directory and get the return as the file names which contains the partiuclar string. (2 Replies)
Discussion started by: thepitzaboy
2 Replies
Login or Register to Ask a Question