Split files based on user input


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Split files based on user input
# 1  
Old 04-06-2015
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 sggest me the best way to do this.

I have tried to to this by using while and foreach loop but got stuck in spliting the files

Code:
while read PxyType
 
do
 
foreach ($PxyType in "${DIR}"/"File.json")
 
----
 
done;

Please help.

Last edited by Don Cragun; 04-06-2015 at 04:42 AM.. Reason: Add CODE tags.
# 2  
Old 04-06-2015
Hi Deena,

Please give a sample input and output. It will help you get a faster response.
# 3  
Old 04-06-2015
Thanks for the reply.

each original file has more than 10000 lines.

Sample Input for PxyType

Code:
<proxyType>CommodityCapFloor</proxyType>
<proxyType>CommodityCapFloor</proxyType>
<proxyType>commSwap</proxyType>
<proxyType>commCap</proxyType>

Output Expected:

For eg, If users inputs "commCap" and it matches 1000 lines in $myfile, I need to have 1000 files (1000 *.json) generated

Wi ll that make clear..

Last edited by Don Cragun; 04-06-2015 at 04:44 AM.. Reason: Add CODE tags, again.
# 4  
Old 04-06-2015
No. It is not clear.
  1. What operating system are you using?
  2. What shell are you using?
  3. How is DIR defined?
  4. How is myfile defined?
  5. How do you determine if a line in $myfile matches the user input?
  6. How is the user input accessed in your program? (Command line argument? Command line option? String read from a file? String prompted for and read from standard input? ...?)
  7. Rather than just showing us code that is not working, describe what you are trying to do with foreach ($PxyType in "${DIR}"/"File.json").
  8. You have said you want to produce thousands of files named File1,2,3..json and *.json. Both of these specifications are ambiguous. What filenames are you trying to produce?
Please show us small, representative sample input files (using CODE tags), sample input the user might supply (using CODE or ICODE tags), and the names and contents of the output files that should be created from those sample inputs (using CODE tags).
# 5  
Old 04-06-2015
Hi,

Im trying this in Linuk OS
BASH shell
DIR and myfile is defined in my script

Code:
DIR="/opt/cdhsit/daas/message-ingestion/messge"
echo "Enter a file name:"
read myfile

user input is accessed by
Code:
echo "Please enter 'ProxyType' :"
read PxyType
 
sed -n '/<proxyType>/,/<\/proxyType>/p' > "${DIR}"/"EndureFiles.json"

through the above I will have the ProxyType details.
This will match with the value in
"
Code:
<dataservices/native/bo_request/trade/proxyProduct>

" --> This is xml path

So, I want the match of each proxyType's to redirect to individual files

Last edited by Scrutinizer; 04-06-2015 at 07:09 AM.. Reason: code tags
# 6  
Old 04-06-2015
Please use code tags as required by forum rules!

This is far from clear to me. On top of what Don Cragun said,
- Where do you use $PxyType?
- Where do you read from $myfile?
- Your sed snippet will NOT consistently retrieve all and only proxytypes. It will (by accident) if supplied with your sample but fail on more general files.
- Nothing in your sample will match what you call the xml path.
- What be the directory structure for your individual files?
- foreach is not a bash construct.
This User Gave Thanks to RudiC For This Post:
# 7  
Old 04-13-2015
I have done this with split command. Thanks
 
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

Split a non delimited file into columns depending on user input

I would like some advice on some code. I want to write a small script that will take an input file of this format 111222233334444555666661112222AAAA 2222333445556612323244455445454545 2334556345643534505435345353453453 (and so on) It will be called as : script inputfile X (where X is... (5 Replies)
Discussion started by: onlyforbopi
5 Replies

4. Shell Programming and Scripting

Split: File into multiple and keeping the same 3 lines from input into all output files

The following code will split the infile into multiple files. However, I need it to insert the same first 3 lines from the original input file into each splitted file. How do I modify my script below to do so: print -n "Enter file name to split? " ; read infile if then echo "Invalid file... (4 Replies)
Discussion started by: mrn6430
4 Replies

5. 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

6. 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

7. Shell Programming and Scripting

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,... (0 Replies)
Discussion started by: DC Slick
0 Replies

8. Shell Programming and Scripting

Split a file into multiple files based on the input pattern

I have a file with lines something like. ...... 123_start ...... ....... 123_end .... ..... 456_start ...... ..... 456_end .... ..... 789_start .... .... 789_end (6 Replies)
Discussion started by: abinash
6 Replies

9. Shell Programming and Scripting

Script to split files based on number of lines

I am getting a few gzip files into a folder by doing ftp to another server. Once I get them I move them to another location .But before that I need to make sure each gzip is not more than 5000 lines and split it up . The files I get are anywhere from 500 lines to 10000 lines in them and is in gzip... (4 Replies)
Discussion started by: gubbu
4 Replies

10. UNIX for Dummies Questions & Answers

split files based on size

I have a few txt files in some directory and I need to check their sizes one by one. If any of them are greater than 5mb then I need to split the file in two. Can someone help? Thanks. (6 Replies)
Discussion started by: khanvader
6 Replies
Login or Register to Ask a Question