Append file based upon user input-- solved


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Append file based upon user input-- solved
# 1  
Old 02-16-2011
Append file based upon user input-- solved

Ok, I have a script with a commandline option that allows the user to add a custom function to the script file. I have tried everything in my limited knowledge of sed to get this to work and keep coming up short. I need sed to search for a line starting with a pattern, I've got that part so far, but now I need it to write to the line directly under it with an argument passed to the script. Right now i have several commandline switches being handled i want the -i switch to allow a user to add what comes next to the script. ex:
Code:
./myscript.sh -i test

should append 'test' to the file and save changes. Kind of like in javascript where you can .appendChild and such. There's a line that starts with foobar that i'd like sed to do this: find the line starting with foobar in myscript.sh and append test after that line. A subsequent 'cat' or 'vim' of that file should reflect test has been appended. I'll even use awk if that's a better answer. Find foobar in myscript.sh and add test after it and save the changes.

---------- Post updated at 12:43 AM ---------- Previous update was at 12:39 AM ----------

oh geez I should have just tried one more time.
Code:
sed -i blahblahbla

---------- Post updated at 03:41 AM ---------- Previous update was at 12:43 AM ----------

ok i've got sed working almost as needed. In my script i have two occurrences of "#CUSTOM", once in the sed command and once as a comment. I need sed to edit the script that its in and do this:
Code:
sed -i '/#CUSTOM/ a\ $aVariable' /path/to/sed/script

But not append it after where sed is locate. Let's say where I want it to append to is line 50, or even just say "the second occurrence".Basically I'm letting the user edit the calling script and add a directory I get from them which i want to become $aVariable. Please help I've been messing with sed all night now. It's either given me a line after both or when i tried some example i saw online it appended it after about 50 lines.

---------- Post updated at 03:54 AM ---------- Previous update was at 03:41 AM ----------

ok fixed the occurrence issue with
Code:
^

before #CUSTOM. I still can't get variables working. Anyone?

---------- Post updated at 04:07 AM ---------- Previous update was at 03:54 AM ----------

nvm fixed it on my own...lol...forgot about the stupid single quotes variable expansion. use " "
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Automaticaly create function based off user input

I am trying to create a bash script that will create new function by using the user input. The below will create the necessary files in the correct format, however when it comes to the # create function I am at a loss. If the name entered was NEWNAME and the genes were GENE1,GENE2 then two files... (0 Replies)
Discussion started by: cmccabe
0 Replies

2. Shell Programming and Scripting

awk command to search based on 5 user input fields

Field1=”” Field2=”” Field3=”” Field4=”” Field5=”” USER INPUT UP TO 5 FIELDS awk -F , '{ if ( $3 == Field1 && $6 == Field2 && $8 == Field3 && $9 == Field4 && $10 == Field5) print $0 }' /tmp/rodney.outD INPUT FILE (Rodney.outD): ... (3 Replies)
Discussion started by: rmerrird
3 Replies

3. Shell Programming and Scripting

Bash to search file based off user input then create new file

In the below bash a file is downloaded when the program is opened and then that file is searched based on user input and the result is written to a new file. For example, the bash is opened and the download.txt is downloaded, the user then enters the id (NA04520). The id is used to search... (5 Replies)
Discussion started by: cmccabe
5 Replies

4. UNIX for Dummies Questions & Answers

Split files based on user input

Hi All, I have a UNIX script which reads "PxyType" (read PxyType) as input from user and a file (eg : "File.json") with the list all PxyType's. Based on the user input for "PxyType" in "File.json", I want to redirect each matched line to a different file ("File1,2,3,..json"). Can you... (7 Replies)
Discussion started by: Deena1984
7 Replies

5. Shell Programming and Scripting

[Solved] Testing Data Type of User Input

hi all i'm new in shell scripting and now i'm working on project and i wanna make a function take input from user and test it's datatype then if correct write it to file i wounder what is the best way to do this ? can i use awk ? edit by bakunin: Please give your threads a meaningful... (5 Replies)
Discussion started by: mohamed91
5 Replies

6. Shell Programming and Scripting

[Solved] Append an header to a tab delimited file

Dear All, I would like to find an automatic way to add a given code which belong to a class at the end of the column , for example this is my input file: 0610009O20Rik V$VMYB_01 310 (+) 1 0.971 v-Myb V$EVI1_04 782 (-) 0.763 0.834 Evi-1 V$ELK1_02 1966 (-) 1 0.984 Elk-1... (4 Replies)
Discussion started by: paolo.kunder
4 Replies

7. Shell Programming and Scripting

Script interacts with user , based on user input it operates

i have a script which takes input from user, if user gives either Y/y then it should continue, else it should quit by displaying user cancelled. #!/bin/sh echo " Enter your choice to continue y/Y OR n/N to quit " read A if then echo " user requested to continue " ##some commands... (7 Replies)
Discussion started by: only4satish
7 Replies

8. Shell Programming and Scripting

Search on date range of file based on user input

Hello I would like to ask for help with a script to search a directory that contains many log files and based on a users input after being prompted, they enter a date range down to the hour which searches the files that contain that range. I dont know how to go about this. I am hoping that the... (5 Replies)
Discussion started by: lostincashe
5 Replies

9. Shell Programming and Scripting

Take user input and then append to another file

Hi, I have below line from the config file $__MR_CONFIG = 'userinput'; I want to get the value from the user for the "userinput" and then append the full line to another file. Can you please let me know how can I proceed on this? I am new to unix and your help is greatly appreciated.... (3 Replies)
Discussion started by: nav123
3 Replies

10. UNIX for Advanced & Expert Users

cut and append the file based on id

Hi, I have a file which have set of rows and has to create separate files based on the id. Eg: 001_AHaris020 001_ATony030 002_AChris090 002_ASmit060 003_AJhon001 Output: I want three files like 001_A.txt, 002_A.txt and 003_A.txt. 001_A.txt should have ... (1 Reply)
Discussion started by: techmoris
1 Replies
Login or Register to Ask a Question