Read and editing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read and editing
# 1  
Old 11-05-2010
Read and editing

I got a text file with the following information:
Quote:
name;IP;User;adress;date
sdasdsad;128.64.0.1;nsans;21-12-2011
dsdaf;128.64.0.1;jjhjhj;21-12-2011
sdasdsad;128.64.0.1;nsans;21-12-2011
ffffffff;128.64.0.1;ttttt;21-12-2011
I need a script that (reads the file) asks the name and modifies his ip given by the user (new ip) and saves the edited information in the same file. I've been trying (I'm new to this) to use cut and grep but I've been unsuccessful.

Thx
# 2  
Old 11-05-2010
Code:
 
sed 's/\([^;]*\);\([^;]*\);/\1 \2 /' file |while read n i r
do
echo "$n $i ? \c"
read i
echo "$n;$i;$r"
done

Narrative: sed get the first two ; separated fields and space delimit them, read the three fields into three variables, ask the question, get the answer, and write the new line out. No room for operator error, though. Maybe let them update it in msword on their PC?
# 3  
Old 11-05-2010
Thx for the help but i cant make that code work (newbie). I made this one. It's still not working but i feel im close.

Quote:
#!/bin/sh

echo "Type the name of the file"
read f

if [ -f $f ]; then
cat $f
else
echo "File not found"
fi

echo "Name"
read maq

cat $f | grep -v "$maq" > newfile.txt
a=cat $f | grep -i "$maq" | cut -f1 -d';'
b=cat $f | grep -i "$maq" | cut -f2 -d';'
c=cat $f | grep -i "$maq" | cut -f3 -d';'
d=cat $f | grep -i "$maq" | cut -f4 -d';'
e=cat $f | grep -i "$maq" | cut -f2 -d';'


echo "New ip"
read b
echo "$a;$b;$c;$d;$e" >> newfile.txt


cat newfile.txt
The part in red is giving me some errors.
# 4  
Old 11-05-2010
Well, there are so many dangers trying to make an editor. First, save the prior file to a .date_HHMMSS extension. If you get a name, it should be all of the first field: "^$name;" and there should be just one [ $(grep -c ... ) = 1 ], before you take the old line out (grep -v . . . <old >new) of the old to make a new file, and put it on at the end with the old fields (echo . . . >>new ).
# 5  
Old 11-06-2010
Code:
echo "Name"
read maq

set -- $(awk -F\; '/'"$maq"'/ {print $1,$2,$3,$4,$2}' $f)

after that, you can use them directly by $1,$2,...$5.
Code:
echo $1
echo $2
...
echo $5

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert vi editing to text editing

Dear Guru's I'm using Putty and want to edit a file. I know we generally use vi editor to do it. As I'm not good in using vi editor, I want to convert the vi into something like text pad. Is there any option in Putty to do the same ? Thanks for your response. Srini (6 Replies)
Discussion started by: thummi9090
6 Replies

2. Shell Programming and Scripting

[Bash] Read History function & Read Arrowkeys

Hi. How can I create a history function? (By "read" command or so) & How can I configure a read command so that the arrow keys are not displayed so funny? (^[[A) Thanks in advance. (4 Replies)
Discussion started by: sinnlosername
4 Replies

3. Shell Programming and Scripting

Read from file and execute the read command

Hi, I am facing issues with the below: I have a lookup file say lookup.lkp.This lookup.lkp file contains strings delimited by comma(,). Now i want to read this command from file and execute it. So my code below is : Contents in the lookup.lkp file is : c_e,m,a,`cd $BOX | ls cef_*|tail... (7 Replies)
Discussion started by: vital_parsley
7 Replies

4. Shell Programming and Scripting

Help with Bash piped while-read and a read user input at the same time

Hi I am new to writing script and want to use a Bash Piped while-read and read from user input. if something happens on server.log then do while loop or if something happend on user input then do while loop. Pseudocode something like: tail -n 3 -f server.log | while read serverline || read... (8 Replies)
Discussion started by: MyMorris
8 Replies

5. UNIX for Dummies Questions & Answers

When reading a csv file, counter to read 20 lines and wait for minute then read next 20 till end

Hello All, i am a newbie and need some help when reading a csv file in a bourne shell script. I want to read 10 lines, then wait for a minute and then do a reading of another 10 lines and so on in the same way. I want to do this till the end of file. Any inputs are appreciated ... (3 Replies)
Discussion started by: victor.s
3 Replies

6. Shell Programming and Scripting

Read Embedded Newline characters with read (builtin) in KSH93

Hi Guys, Happy New Year to you all! I have a requirement to read an embedded new-line using KSH's read builtin. Here is what I am trying to do: run_sql "select guestid, address, email from guest" | while read id addr email do ## Biz logic goes here done I can take care of any... (6 Replies)
Discussion started by: a_programmer
6 Replies

7. UNIX for Advanced & Expert Users

read() wont allow me to read files larger than 2 gig (on a 64bit)

Hi the following c-code utilizing the 'read()' man 2 read method cant read in files larger that 2gig. Hi I've found a strange problem on ubuntu64bit, that limits the data you are allowed to allocate on a 64bit platform using the c function 'read()' The following program wont allow to allocate... (14 Replies)
Discussion started by: monkeyking
14 Replies

8. Programming

Cannot read a file with read(fd, buffer, buffersize) function

# include <stdio.h> # include <fcntl.h> # include <stdlib.h> # include <sys/stat.h> int main(int argc, char *argv) { int fRead, fPadded, padVal; int btRead; int BUFFSIZE = 512; char buff; if (argc != 4) { printf ("Please provide all of the... (3 Replies)
Discussion started by: naranja18she
3 Replies

9. Shell Programming and Scripting

Help with editing file

I have a file of 100000 records. This file is created by concatenation of two files. I want to edit this file from record number 80,000 till the end and add "|N" for each record . How can I acheive this. Please suggest (4 Replies)
Discussion started by: dsravan
4 Replies

10. UNIX for Dummies Questions & Answers

Editing files

hi i would like to know whether i can delete a part of a file in C for eg. if my file contained 1234567890 and i want to delete 456 so that it becomes 1237890 is there a way i can do this. well, one way i can achieve this is by creating a new file, copy whatever i want, then... (2 Replies)
Discussion started by: sameersbn
2 Replies
Login or Register to Ask a Question