Adding user name to file, and then displaying new line number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding user name to file, and then displaying new line number
# 1  
Old 04-10-2016
Adding user name to file, and then displaying new line number

Hi all - I'm completely stumped by a script I'm working on...

The short version is I have a file called 'lookup' and in it are hundreds of names (first and last). I have a script that basically allows the user to enter a name, and what I need to have happen is something like this:

Record <name> added at line 5.

so lookup looks like this:
Code:
Homer Simpson
Jane Doe
John Doe
Barney Rubble

(etc...)

User then adds 'Fred Flinstone' (sorry for the use of creative names...)
And it would come back (using the file above)
Record Fred Flinstone added at line 3

So basically (there's a lot of stuff snipped out obviously) but the script basically looks like this:

Code:
echo -n "Enter name:  "
read name
echo $name >> lookup

and there's where I'm stuck. My next line is something like this:
Code:
echo -n "Record $name at line "

And that's it - I can search the lookup file using: nawk /$name/ lookup - but I can't figure out how to get the damned line number Smilie

Any help would be appreciated...

EDIT: Sorry, I forgot to add that I'm sorting the file by last names.... (that's the reason why - using the example above - 'fred flinstone' would go in at line 3 (obviously sorted file would have 'jane doe, john doe, fred flinstone, barney rubble, homer simpson' in that order...)

Last edited by Scrutinizer; 04-10-2016 at 11:40 AM.. Reason: code tags
# 2  
Old 04-10-2016
You can use wc -l < file , so after the line is added, you'll know how many lines are in the file..

Last edited by Scrutinizer; 04-10-2016 at 12:44 PM..
# 3  
Old 04-10-2016
Code:
nl file |awk .................

will add line numbers to the file, then adjust whatever awk script you have to account for the added first field.
# 4  
Old 04-10-2016
How about (quick and dirty)
Code:
echo $name | sort -k2 - file | awk "/$name/ {print \"record $name at \" NR}"
record Fred Flinstone at 3

# 5  
Old 04-10-2016
Quote:
Originally Posted by RudiC
How about (quick and dirty)
Code:
echo $name | sort -k2 - file | awk "/$name/ {print \"record $name at \" NR}"
record Fred Flinstone at 3

So this works perfectly with one exception:

EDIT: So this is a weird bug (sorta): I was using a test file (with made up names) and running this command against my file - and it appears to be matching either the first or last name, and if one of them match - it works...

ie.
Code:
name=Destiny Cat
<the command above>

Destiny Cat found at line 3
Destiny Cat found at line 5 (but this is actually Destiny Dog)

So it found Destiny in both cases - even though the last name on one is Cat and the other is Dog. ie. it's not searching for the full name ... I'll keep toying around with it. But if someone sees a quick solution - I'll be back. Smilie

Moderator's Comments:
Mod Comment Please use code tags as required by forum rules!

Last edited by RudiC; 04-11-2016 at 03:39 AM.. Reason: Added code tags (again!)
# 6  
Old 04-10-2016
Quote:
Originally Posted by RudiC
How about (quick and dirty)
Code:
echo $name | sort -k2 - file | awk "/$name/ {print \"record $name at \" NR}"
record Fred Flinstone at 3

why it deletes all the contents of the file when I add" > file" after sort? "sort -k2 file > file".If the name already exist then we dont add it. how should we do?Thank you!
# 7  
Old 04-10-2016
Quote:
Originally Posted by hhdzhu
why it deletes all the contents of the file when I add" > file" after sort? "sort -k2 file > file".If the name already exist then we dont add it. how should we do?Thank you!
If you're saying that only want to add the given name to your file (without checking to see whether or not that name was already in the file, AND you don't need the line where it was added, the way to do that would be:
Code:
echo $name | sort -k2 - file -o file

and if you wanted to only add that name if it was not already in the file:
Code:
echo $name | sort -u -k2 - file -o file

When you use the command line:
Code:
sort -k2 file > file

the shell sees the redirection (> file) and creates a file named file (destroying any contents that may have existed in that file) and then starts the sort utility. The sort utility will then open the file named as a command-line argument (in this case also named file which, after the shell has redirected the output, is now an empty file).
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding filename and line number from multiple files to final file

Hi all, I have 20 files (file001.txt upto file020.txt) and I want to read them from 3rd line upto end of file (line 1002). But in the final file they should appear to start from line 1. I need following kind of output in a single file: Filename Line number 2ndcolumn 4thcolumn I... (14 Replies)
Discussion started by: bioinfo
14 Replies

2. Shell Programming and Scripting

adding line number to *end* of records in file

Given a file like this: abc def ghi I need to get to somestandardtext abc1 morestandardtext somestandardtext def2 morestandardtext somestandardtext ghi3 morestandardtext Notice that in addition to the standard text there is the line number added in as well. What I conceived is... (4 Replies)
Discussion started by: edstevens
4 Replies

3. UNIX for Dummies Questions & Answers

Deleting "user input line number" from a file using sed

Hi I want to delete a line from a txt file for which the line number is user input. Say when user selects 19, the 19th line would be deleted from the file. Can anyone please provide me with a sed one liner for the same... I tried sed -i. The interaction would be like this Enter the line to... (1 Reply)
Discussion started by: sudeep.id
1 Replies

4. Shell Programming and Scripting

editing line in text file adding number to value in file

I have a text file that has data like: Data "12345#22" Fred ID 12345 Age 45 Wilma Dino Data "123#22" Tarzan ID 123 Age 33 Jane I need to figure out a way of adding 1,000,000 to the specific lines (always same format) in the file, so it becomes: Data "1012345#22" Fred ID... (16 Replies)
Discussion started by: say170
16 Replies

5. UNIX for Dummies Questions & Answers

How to read contents of a file from a given line number upto line number again specified by user

Hello Everyone. I am trying to display contains of a file from a specific line to a specific line(let say, from line number 3 to line number 5). For this I got the shell script as shown below: if ; then if ; then tail +$1 $3 | head -n $2 else ... (5 Replies)
Discussion started by: grc
5 Replies

6. Shell Programming and Scripting

Displaying lines of a file which have the highest number?

Hello Wondering if anybody may be able to advise on how I can filter the contents of the following file: <object_name>-<version> <Instance> GM_GUI_code.fmb-4 1 GM_GUI_code.fmb-5 1 GM_GUI_code.fmx-4 ... (7 Replies)
Discussion started by: Glyn_Mo
7 Replies

7. Shell Programming and Scripting

merging line and adding number

I having file below o/p ibapp311dg,,20480,s,,,,,,,,, test,,20480,s,,,,,,,,, test,,20480,s,,,,,,,,, ibapp311dg,,20480,s,,,,,,,,, I want to to chk unique word line in the first field seperated by , as well as addup corressponding the number in field for each unique word like ibapp311dg... (8 Replies)
Discussion started by: tarunn.dubeyy
8 Replies

8. Shell Programming and Scripting

Displaying number of lines from file

Hi, I am using below command to display the number of line, but its returning no of lines along with file name. But i want only no of line in the variable p. Please help me on this? p=`wc -l "text file"` echo "$p" (6 Replies)
Discussion started by: shivanete
6 Replies

9. Shell Programming and Scripting

Adding a columnfrom a specifit line number to a specific line number

Hi, I have a huge file & I want to add a specific text in column. But I want to add this text from a specific line number to a specific line number & another text in to another range of line numbers. To be more specific: lets say my file has 1000 lines & 4 Columns. I want to add text "Hello"... (2 Replies)
Discussion started by: Ezy
2 Replies

10. UNIX for Dummies Questions & Answers

displaying the last line of the file

hi... i need to display the last line of the file and capture the line in to a variable in unix envt.(not the perl ones)... please help (8 Replies)
Discussion started by: lmadhuri
8 Replies
Login or Register to Ask a Question