简体   繁体   中英

Merging Files In Bash

I have a script file that uses import and load commands to bring in scripts from other files. I am trying to convert these scripts to a new language but it is hard to follow the variables through all of the different imports/loads. I am trying to write a BASH script to go through a file and import all the text from the included/imported files inline where it was called.

Final Solution Used!

sed -e 's/\\/\//g' test.restomatic | awk '{if ($1 ~ /^(LOAD|IMPORT)/) system("cat " $2); else print;}' > /cygdrive/c/bashScripts/testing.txt

Sample of Original File:

LOAD ..\..\..\..\GlobalSuccess\IdentityShardCreate_Success.rcq
REPLACE /rest-client/request/url/*shard* "//{$OKEnv}"
EXPECT HEADER SERVER
EXPECT HEADER DATE
EXPECT RETURN-CODE 201
RUN
IMPORT ..\..\..\..\GlobalSuccess\IdentityEnvironmentCreateNewEnvironmentFromShard_Success.rcq
REPLACE /rest-client/request/url/*shard* "//{$OKEnv}/environment"
EXPECT HEADER Server
EXPECT HEADER Date
EXPECT RETURN-CODE 204
RUN
ASSIGN ShardTypeControl 1
LOOP ShardTypeControl
ASSIGN OKShard2 {$ShardNames[{$ShardTypeControl}]}
LOAD ..\..\..\..\GlobalSuccess\IdentityEnvironmentAddShardToEnvironment_Success.rcq
REPLACE /rest-client/request/url/*shard* "//{$OKShard2}/environment/{$OKEnv}"

Example of desired results of output file:

Original File (OF) Start
(OF) Line 1
(OF) Line 2
(OF) Line 3
Line 1 of Imported File - would have been (OF) line 4
Line 2 of Imported File
(OF) Line 5
(OF) Line 6
Line 1 of Loaded File - would have been (OF) line 7
Line 2 of Loaded File
Line 3 of Loaded File
(OF) Line 8
Line 1 of Second Imported File - would have been (OF) line 9
Line 2 of Second Imported File

所有这些都可以在单个awk命令中组合,如下所示:

awk '{if ($1 ~ /^(LOAD|IMPORT)/) {gsub(/\\/, "/", $2); system("cat " $2)} else print}' test.txt > output.txt
while IFS= read -r line; do
  case "$line" in
    LOAD *|IMPORT *) cat "${line#* }" ;;
    *) echo "$line" ;;
  esac
done < original.file

Some commentary on the code in the question:

cat /path/original.file | while read $line; do 
if[grep 'IMPORT\|LOAD' $line]; then               
working on it  
else  
working on it: output to file /path/testing.txt  
done
  • Should be while read line (without the $ )
  • [ is a command and requires spaces before and after: if [ grep '...' ... ]; then if [ grep '...' ... ]; then
  • to have grep read the contents of a variable, use bash's "here-string" redirection
    grep 'IMPORT\\|LINE' <<< "$line"
  • missing the terminating keyword fi for if statement
  • always quote variable references, unless you specifically want the side-effects of leaving the quotes off.

From what I can understand here, you are trying to inline the IMPORTs in some scripting language you are using (which you haven't specified) so that you'll end up with a single file that contains the entire script, so that you can then go about attempting to convert said script into another (unspecified) language.

The bash script you have written would end up concatenating all the files mentioned in test.txt into an output file in the order in which they were mentioned -- it would not result in the mix of original and imported text that you are after.

I would suggest an entirely different approach. Use sed to convert the IMPORT statements either into cpp #include statements or the equivalent from m4, and then run the resulting file through cpp or (respectfully) through m4. It is a bit of a hack, but it will produce exactly the result you are looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM