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

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

Please help.

Hi Deena,

Please give a sample input and output. It will help you get a faster response.

Thanks for the reply.

each original file has more than 10000 lines.

Sample Input for PxyType

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

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

Hi,

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

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

user input is accessed by

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
"

<dataservices/native/bo_request/trade/proxyProduct>

" --> This is xml path

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

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.

I have done this with split command. Thanks

For other people reading this forum who may be trying to solve a similar problem, would you share your solution so we can all learn from your success?