how to read a file using shell ??


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to read a file using shell ??
# 1  
Old 09-16-2007
how to read a file using shell ??

Hello

i have a file in directory which has some values like this

lets assume the file is $p
ttry_rtyy_trree

and i am using a file as an input as ($Y) in a shell once i start the shell it should read the file $p and $Y once those lines are matched in $Y i want to take a copy of that line and lines which are below 7 lines and put it in a output file ($Z)
$Y has this
ttry_rtyy_trree
66341251111
14567888888
12323434343
23232454544
2323234354
354545476767
67676767676676
9834587957457439
100192903203239

and my Output file after extracting should be exactly like this

ttry_rtyy_trree
66341251111
14567888888
12323434343
23232454544
2323234354
354545476767

Thanks!!
# 2  
Old 09-16-2007
Is this homework?

You can do this using a "while read" loop and an if that matches for the line pattern you want, then printing the next seven lines by reading them repeatedly. You can also do this using awk, where after you get a match, run a loop where you print and getline repeatedly.
# 3  
Old 09-16-2007
this is not a homework can you please give me the sample code


i am new to this shell programming area .. designing a script for my work
# 4  
Old 09-17-2007
In that case...

Code:
>$Z               #truncate file in $Z to ensure that we start with an empty file
while read line; do
   awk '/'$line'/ {for(i=0;i<7;i++) {print; getline}}' $Y >> $Z #actual work here
done < $p      #read file in $p line-by-line and process each line in while loop

This will work if the pattern in $line occurs only once in the file. If the pattern repeats, you will get multiple outputs for each pattern that is repeated.
# 5  
Old 09-17-2007
Hi.

This is how one could do this with sed:
Code:
#!/usr/bin/env sh

# @(#) s1       Demonstrate match and copy with sed.

set -o nounset
echo

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

version bash sed

echo

cat >data1 <<'EOF'
a
1a
b
1b
c
1c
a
2a
b
2b
EOF

# Search for pattern "a" at beginning of line, then copy it and 1 more line.

sed -n -e '/^a/,+1p' data1

exit 0

Producing:
Code:
% ./s1

GNU bash, version 2.05b.0(1)-release (i386-pc-linux-gnu)
GNU sed version 4.1.2

a
1a
a
2a

Change to suit your situation, see man sed for details ... cheers, drl
# 6  
Old 09-18-2007
Thanks for the replies..

but i have some issues here ..

i have a mainfile (backup file) a.txt

and i have a constant file b.txt which will be added or deleted

lets assume i have the following contents in a.txt :

unix:workinshellscripting
samplethings
sample
condition
success
failure

unix:shell
cool
ice
work
place
history

and b.txt has
workinshellscripting

once i run the shell script it should read the param file and read the value in the backup files

and once the string is found it has to copy the contents (5 lines below) and paste it in an output file

hence my outputfile has these

unix:workinshellscripting
samplethings
sample
condition
success
failure
# 7  
Old 09-18-2007
Quote:
Originally Posted by blowtorch
In that case...

Code:
>$Z               #truncate file in $Z to ensure that we start with an empty file
while read line; do
   awk '/'$line'/ {for(i=0;i<7;i++) {print; getline}}' $Y >> $Z #actual work here
done < $p      #read file in $p line-by-line and process each line in while loop

This will work if the pattern in $line occurs only once in the file. If the pattern repeats, you will get multiple outputs for each pattern that is repeated.
this gives syntax error

Bailing out option .. any help on this??

Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell Script to Read the given file contents into a merged one file

Like to have shell script to Read the given file contents into a merged one file with header of path+file name followed by file contents into a single output file. While reading and merging the file contents into a single file, Like to keep the format of the source file. ... (4 Replies)
Discussion started by: Siva SQL
4 Replies

2. Shell Programming and Scripting

Read csv file, convert the data and make one text file in UNIX shell scripting

I have input data looks like this which is a part of a csv file 7,1265,76548,"0102:04" 8,1266,76545,"0112:04" I need to make the output data should look like this and the output data will be part of text file: 7|1265000 |7654899 |A| 8|12660000 |76545999 |B| The logic behind the... (6 Replies)
Discussion started by: RJG
6 Replies

3. Shell Programming and Scripting

Shell script to read specified value from file and echo to the same location to other file.

Hello. I want to to backup some "default:" values from a file do some other job and after restore that "default:" values back. The problem is that the source and destination file has a lot of default: strings in it but with different values... So.. Here is an example: A part of my source... (6 Replies)
Discussion started by: ausdim
6 Replies

4. UNIX for Dummies Questions & Answers

Shell script to read lines in a text file and filter user data Shell Programming and Scripting

sxsaaas (3 Replies)
Discussion started by: VikrantD
3 Replies

5. Shell Programming and Scripting

Shell script to read file and check file type

Hi, I have a file with few values in it. I need script help to read file line by line and check: 1/if it's a file (with extension eg .java .css .jar etc ) or 2/if it's a file without extension and treat it as a directory and then check if the directory exists in working copy else create one... (6 Replies)
Discussion started by: iaav
6 Replies

6. Shell Programming and Scripting

Need shell script to read two file at same time and print out in single file

Need shell script to read two file at same time and print output in single file Example I have two files 1) file1.txt 2) file2.txt File1.txt contains Aaa Bbb Ccc Ddd Eee Fff File2.txt contains Zzz Yyy Xxx (10 Replies)
Discussion started by: sreedhargouda
10 Replies

7. Shell Programming and Scripting

To read and separate number and words in file and store to two new file using shell

hi, I am a begginer in unix and i want to know how to open a file and read it and separate the numbers & words and storing it in separate files, Using shell scripting. Please help me out for this. Regards S.Kamakshi (2 Replies)
Discussion started by: kamakshi s
2 Replies

8. Shell Programming and Scripting

Post Shell programming: Question about source a file and read data from the file

This is shell programming assignment. It needs to create a file called .std_dbrc contains STD_DBROOT=${HOME}/class/2031/Assgn3/STD_DB (which includes all my simple database files) and I am gonna use this .std_dbrc in my script file (read the data from the database files) like this: .... (3 Replies)
Discussion started by: ccwq
3 Replies

9. Shell Programming and Scripting

Read words from file and create new file using K-shell.

Hi All, Please help me in creating files through K-shell scripts. I am having one file in this format. OWNER.TABLE_NAME OWNER.TABLE_NAME1 OWNER1.TABLE_NAME OWNER1.TABLE_NAME1 I want to read the above file and create new file through k shell script. The new file should looks like this.... (4 Replies)
Discussion started by: bsrajirs
4 Replies

10. UNIX for Advanced & Expert Users

How to read from a file in C shell?

Hi All, I want to read some number from a file into a variable in C shell as follows. I know that the syntax in ksh is read x < file name I dont know what's the equivalent for this in C Shell Can some one pls. help me out? Thanks in advance raju (2 Replies)
Discussion started by: rajugp1
2 Replies
Login or Register to Ask a Question