Copy from/to file, if arg 'ok'


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copy from/to file, if arg 'ok'
# 1  
Old 03-25-2014
Copy from/to file, if arg 'ok'

Hello, I've just started with shell scripting, and I need som assistance!
Basicly what I want to do is copy names from one file to another, when running the scrip the user should be asked about the status of these names.
Let me demonstrate:

file:
Code:
Name: John, Gender: male 
Name: Jane, Gender: female

// when the script is running
Code:
Is Johns status ok y/n? n
Is Janes status ok y/n? y

newfile:
Code:
Name: John, Gender: male, Status: -
Name: Jane, Gender: female, Status: ok

I know how to copy a file, but I really don't even know where to start here! Help is appreciated!

Last edited by Don Cragun; 03-25-2014 at 05:41 PM.. Reason: Add CODE tags.
# 2  
Old 03-25-2014
Is this a homework assignment?
What shell are you using?
Have you read the description of the read and printf built-in utilities on your shell's man page?
# 3  
Old 03-25-2014
Quote:
Originally Posted by Don Cragun
Is this a homework assignment?
What shell are you using?
Have you read the description of the read and printf built-in utilities on your shell's man page?
I am using the bash shell in ubuntu, it's an exercise, I have not read the printf I will do it now, thx
# 4  
Old 03-25-2014
What have you done so far?
# 5  
Old 03-25-2014
Quote:
Originally Posted by sea
What have you done so far?
Well, I have learned how to copy from one file to another, but I'd like to know how to add a field in the new file, depending on what the user enters. I understand I might jump into things alittle fast.. :/ so if you have tips on tutorials that be great
# 6  
Old 03-26-2014
In a while loop we read the input file line by line, and write each line to the output file.
We associate descriptors 3 & 4 rather than the default 0 & 1, so another read/print (for interactive i/o) won't interfere
Code:
#!/bin/sh
while read line <&3
do
  name=`expr "$line" : "Name: *\([^,]*\)"`
  printf "Is %ss status okay y/n? " $name
  read status
  if [ "$status" = "y" ]
  then
    status="ok"
  else
    status="-"
  fi
  printf "$line, Status: $status\n" >&4
done 3< file 4> newfile


Last edited by MadeInGermany; 03-26-2014 at 08:45 AM.. Reason: 0 & 1 not 1 & 2
These 2 Users Gave Thanks to MadeInGermany For This Post:
# 7  
Old 03-26-2014
Quote:
Originally Posted by MadeInGermany
In a while loop we read the input file line by line, and write each line to the output file.
We associate descriptors 3 & 4 rather than the default 0 & 1, so another read/print (for interactive i/o) won't interfere
Code:
#!/bin/sh
while read line <&3
do
  name=`expr "$line" : "Name: *\([^,]*\)"`
  printf "Is %ss status okay y/n? " $name
  read status
  if [ "$status" = "y" ]
  then
    status="ok"
  else
    status="-"
  fi
  printf "$line, Status: $status\n" >&4
done 3< file 4> newfile

This was a huge help, ty! I can now finally continue with this script! (.. don't know if I should be happy or upset for making it look so easy Smilie )
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pass command line arg to sql file

Hi all, How to pass the command line argument to a sql file Script: #!/bin/ksh if ] ; then test.sql fi My Sql Informix DB: echo "select * from table where col1 = 2234 and col2 = '$3'"|dbaccess ddname But im getting `:' unexpected error (5 Replies)
Discussion started by: Roozo
5 Replies

2. UNIX for Dummies Questions & Answers

Arg list too long

Hello All, I am trying to find a file name with .sh exention from a list of .dat files inside a directory. find /app/folder1/* -name '*.dat'| xargs grep '.sh' ksh: /usr/local/bin/find: arg list too long Please help me finding the command. Thanks (3 Replies)
Discussion started by: tkhan9
3 Replies

3. Shell Programming and Scripting

how to copy the directory but not copy certain file

Hi experts cp bin root src /mnt but not copy bin/bigfile any help? ( I post this thread in the "redhat" forum wrongly, I don't know how to withdraw that question in that wrong forum) Thanks (6 Replies)
Discussion started by: yanglei_fage
6 Replies

4. UNIX for Dummies Questions & Answers

Space in a arg

Hi, When i am running a script it considers the below as mulitple arguments. There is any command which can ignore the spaces. I tried by using sed like below to ignore but it doesnt work. sed 's/ /\\ /g'100% Haddock Fillets Battered 500G-small.gif ~vino (3 Replies)
Discussion started by: vino_hymi
3 Replies

5. Programming

warning: int format,pid_t arg (arg 2)

I try following code under Solaris10,like follows: int glob = 6; int main(void) { int var; pid_t pid; var = 88; printf("before vfork\n"); if ((pid = vfork()) < 0) { err_sys("vfork error"); } else if (pid == 0) { glob++; var++; _exit(0); } ... (1 Reply)
Discussion started by: konvalo
1 Replies

6. Shell Programming and Scripting

arg list too long

Hi, Help. I have a file that contains a list of users in a file. I want to cat the content of the file and feed it into sed to a preformated report. The error I got is "ksh: /usr/bin/sed: arg list too long" My method below. A=`cat FILE1.txt` B=`echo $A` sed "s#USERLIST#$B#" FILE2 >... (2 Replies)
Discussion started by: Zenwork
2 Replies

7. AIX

file system space arg !!! and confused

Aix 5.2 on a P510 RS6000 My /usr filesystem is sized at 3.8 gb. Check me if I'm wrong here but this should be enough space for /usr? When I installed the o/s I just followed the defaults, didn't add any 3rd party apps, least that I know of. My problem is that it's running at 90% capacity. ... (2 Replies)
Discussion started by: Westy564
2 Replies

8. Shell Programming and Scripting

How to insert the 1st arg into the middle of the file

I wrote a script like #!/bin/bash echo $1 > temp cat $2 >> temp mv temp $2 now I have problem appending the above script(only using bash shell) so that it now inserts the first argument into the middle of the file. I have tried using $(('wc -l < file' / 2 )) but invain so could any one... (4 Replies)
Discussion started by: boris
4 Replies
Login or Register to Ask a Question