Reading an Input file and searching for occurrences WIHOUT SED or AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading an Input file and searching for occurrences WIHOUT SED or AWK
# 1  
Old 05-08-2008
Reading an Input file and searching for occurrences WIHOUT SED or AWK

Hi people. I am new to shell scripting, so I need a little help. I want to create a script named that takes an argument as a file, Read the input file and look for occurrences of the current username (say abc.xyz) who is executing the script. On finding an occurrence of the username take that line and append it to a file and display a line number and a bracket against the saved line. WITHOUT USING SED OR AWK

for eg: the newly created file should look similar to this:
1) PWD=/home/abc.xyz
2) USER=abc.xyz
3) ... abc.xyz
4)..

i have created a script that is doing just about the same thing.

echo "Precisely Enter the file path";
read filename;

varible=$(whoami);

grep -n $varible $filename >> Userfile;

more Userfile;



but the output of the file is coming as (obviously coz of -n)
9:jhhf=/kjsd/abc.xyz
14:var/spool/mail/abc.xyz
15:/home/abc.xyz
21:/home/abc.xyz
23:abc.xyz

I BASICALLY WANT TO REORDER THE OUTPUT IN A SEQUENCE AS

1)
2)
3)
4)

any help would really appreciated!

is there a command that does that.
# 2  
Old 05-08-2008
Code:
echo "Precisely Enter the file path"
read filename
varible=$(whoami)

grep $varible $filename > Userfile

nl Userfile

# 3  
Old 05-08-2008
Quote:
Originally Posted by kartikkumar84@g
Hi people. I am new to shell scripting, so I need a little help. I want to create a script named that takes an argument as a file, Read the input file and look for occurrences of the current username (say abc.xyz) who is executing the script. On finding an occurrence of the username take that line and append it to a file and display a line number and a bracket against the saved line. WITHOUT USING SED OR AWK

for eg: the newly created file should look similar to this:
1) PWD=/home/abc.xyz
2) USER=abc.xyz
3) ... abc.xyz
4)..

i have created a script that is doing just about the same thing.

echo "Precisely Enter the file path";
read filename;

varible=$(whoami);

grep -n $varible $filename >> Userfile;

more Userfile;



but the output of the file is coming as (obviously coz of -n)
9:jhhf=/kjsd/abc.xyz
14:var/spool/mail/abc.xyz
15:/home/abc.xyz
21:/home/abc.xyz
23:abc.xyz

I BASICALLY WANT TO REORDER THE OUTPUT IN A SEQUENCE AS

1)
2)
3)
4)

any help would really appreciated!

is there a command that does that.
hi jim thank you for your reply. but

1) i need to number the lines only in this format 1)... 2)... 3).... Do you think i will need to use a loop or something and then keep appending the occurrences as they come in a new file..also i cant use temp files.

2) also the nl command is only numbering the first uniqe lines in the files. i have a file that has 30 lines repeated 5 times and 'nl' is only numbering the 1st 30 lines for some reason.

cheers...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk or sed script to count number of occurrences and creating an average

Hi Friends , I am having one problem as stated file . Having an input CSV file as shown in the code U_TOP_LOGIC/U_HPB2/U_HBRIDGE2/i_core/i_paddr_reg_2_/Q,1,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,0,0... (4 Replies)
Discussion started by: kshitij
4 Replies

2. Shell Programming and Scripting

awk - sed / reading from a data file and doing algebraic operations

Hi everyone, I am trying to write a bash script which reads a data file and does some algebraic operations. here is the structure of data.xml file that I have; 1 <data> 2 . 3 . 4 . 5 </data> 6 <data> 7 . 8 . 9 . 10</data> etc. Each data block contains same number of lines (say... (4 Replies)
Discussion started by: hayreter
4 Replies

3. Shell Programming and Scripting

Help with reading two input files in awk

Hello, I'm trying to write an awk program that reads two files inputs. example, file 1: 0.00017835 0.000176738 0.00018811 0.000189504 0.000188155 0.000180065 0.000178991 0.000178252 0.000182513 file 2: 1.7871769E-05 1.5139576E-16 1.5140196E-16 1.5139874E-16 1.7827407E-04 ... (5 Replies)
Discussion started by: joseamck
5 Replies

4. Shell Programming and Scripting

awk- reading input file twice

Hello, I've been trying to come up with a solution for the following problem; I have an input file with two columns and I want to print as an output the first column without any changes but for the second column, I want to divide it by its last value. Example input: 1 9 2 10 3 11 4 12 5... (14 Replies)
Discussion started by: acsg
14 Replies

5. Shell Programming and Scripting

Searching a file using awk or sed

I want to search a file in a specific location and I don't want to use find command. I want to give the path also where the file is for searching it. Pls help (3 Replies)
Discussion started by: maitree
3 Replies

6. Shell Programming and Scripting

awk script - reading input lines

Can I do something like, if($0==/^int.*$/) { print "Declaration" } for an input like: int a=5; If the syntax is right, it is not working for me, but I am not sure about the syntax. Please help. Thanks, Prasanna (1 Reply)
Discussion started by: prasanna1157
1 Replies

7. Shell Programming and Scripting

Reading from input with sed

I am trying to edit a file in shell script using sed. I need to get the input from command line suppose script.sh sed"/s place=/place=california/g" > /root/user/mark.txt echo " place changed " the above code searches for string place in the file mark.txt and replaces with place=... (5 Replies)
Discussion started by: sriki32
5 Replies

8. Shell Programming and Scripting

Reading from standard input with awk

Hello, Could somebody please give me an awk example on how to read from the standard input. It means as the "read" function in Korn shell. Thx in advance ... (3 Replies)
Discussion started by: rany1
3 Replies

9. Shell Programming and Scripting

Reading specific contents from 1 input files and appending it to another input file

Hi guys, I am new to AWK and unix scripting. Please see below my problem and let me know if anyone you can help. I have 2 input files (example given below) Input file 2 is a standard file (it will not change) and we have to get the name (second column after comma) from it and append it... (5 Replies)
Discussion started by: sksahu
5 Replies

10. Shell Programming and Scripting

awk reading 2 input files but not getting expected value

I'm reading 2 input files but not getting expected value. I should get an alpha value on file_1_data but not getting any. Please help. >cat test6.sh awk ' FILENAME==ARGV { file_1_data=$0; print "----- 1 Line " NR " -----" $1; next } FILENAME==ARGV { file_2_data=$0; print "----- 2... (1 Reply)
Discussion started by: pdtak
1 Replies
Login or Register to Ask a Question