0

I have a list of the locations of each text file that need this done inside add.txt

For example inside add.txt I have the following

/root/QuestDiary/MapQuest_Def/Mon_Umyounkijang.txt
/root/Market_Def/06Inn_SamakwAr-5.txt

I need a script to open every text file in that list and add ;; to the top and bottom of each of them. It also must also open files/folders that have capitals, it's listed as /root/Market_Def/06Inn_SamakwAr-5.txt in the file but the actual file name is /root/Market_DEF/06Inn_SamakWar-5.txt

06Inn_SamakWar-5.txt currently looks like

%100
+40
+1

[@main]
#IF
checkpkpoint 2

#SAY
#INCLUDE [..\Convert_Def\Market_Def\01Meet_BichonSung-10.txt] @Convert_01Meet_BichonSung-10_0


#ELSESAY
#INCLUDE [..\Convert_Def\Market_Def\01Meet_BichonSung-10.txt] @Convert_01Meet_BichonSung-10_1

[@buy]

#SAY
#INCLUDE [..\Convert_Def\Market_Def\01Meet_BichonSung-10.txt] @Convert_01Meet_BichonSung-10_2

[@sell]

#SAY
#INCLUDE [..\Convert_Def\Market_Def\01Meet_BichonSung-10.txt] @Convert_01Meet_BichonSung-10_3

[Goods]

Jerk 1000 1

[@TalkToQuest]
#CALL [NPCQuest_Def\02Weapon_BichonSung-10.txt] @NPCQuest_Check

[@everythingreward]
#CALL [System\TotalRewardSystem.txt] @TotalRewardSystem

It should change to

;;
%100
+40
+1

[@main]
#IF
checkpkpoint 2

#SAY
#INCLUDE [..\Convert_Def\Market_Def\01Meet_BichonSung-10.txt] @Convert_01Meet_BichonSung-10_0


#ELSESAY
#INCLUDE [..\Convert_Def\Market_Def\01Meet_BichonSung-10.txt] @Convert_01Meet_BichonSung-10_1

[@buy]

#SAY
#INCLUDE [..\Convert_Def\Market_Def\01Meet_BichonSung-10.txt] @Convert_01Meet_BichonSung-10_2

[@sell]

#SAY
#INCLUDE [..\Convert_Def\Market_Def\01Meet_BichonSung-10.txt] @Convert_01Meet_BichonSung-10_3

[Goods]

Jerk 1000 1

[@TalkToQuest]
#CALL [NPCQuest_Def\02Weapon_BichonSung-10.txt] @NPCQuest_Check

[@everythingreward]
#CALL [System\TotalRewardSystem.txt] @TotalRewardSystem

;;
Rarara221
  • 47
  • 5

2 Answers2

2

With GNU sed:

sed -e '1i ;;' -e '$a ;;'

It is composed of two sed expressions:

  • 1i ;; inserts (i) ;; (newline embedded) before the first line (1)

  • $a ;; appends (a) ;; (newline embedded) after the last line ($)

Example:

$ cat foo.txt
sdas
adas

$ sed -e '1i ;;' -e '$a ;;' foo.txt 
;;
sdas
adas
;;

As you have all the files in add.txt, assuming no filename contains any character of IFS (space, tab, newline by default) nor wildcard charcters (*, ?, []), you can do the following to edit the files in place with a .bak extension as backup of the original:

sed -i.bak -e '1i ;;' -e '$a ;;' -- $(cat add.txt)

Without a backup:

sed -i -e '1i ;;' -e '$a ;;' $(cat add.txt)

When -i is used the files are taken separately instead of a single stream so we are good to go. Without -i, we need to use the -s option to get separated streams for files.

Or read the filenames, separated by newline, and do operation one by one, with backup:

while IFS= read -r f; do sed -i.bak -e '1i ;;' -e '$a ;;' -- "$f"; done <add.txt

without backup:

while IFS= read -r f; do sed -i -e '1i ;;' -e '$a ;;' -- "$f"; done <add.txt
heemayl
  • 56,300
  • 1
    I did sed -e '1i ;;' -e '$a ;;' foo.txt and all my text files where in foo.txt but it done nothing to them. – Rarara221 Oct 14 '16 at 19:17
  • 1
    You need to include the -i option to get sed to work on the files. So sed -i -e '1i ;;' -e '$a ;;' should do it. This is assuming you're using the GNU version of sed (if you're running a Linux OS you're probably using GNU sed, but probably not if you're using osx for example). – evilsoup Oct 14 '16 at 19:21
  • sed -i -e '1i ;;' -e '$a ;;' foo.txt made no changes to the files listed in foo.txt – Rarara221 Oct 14 '16 at 19:22
  • Sorry, it did add ;; to the top and bottom of foo.txt but the script needs to do that to the files that are listed inside foo.txt – Rarara221 Oct 14 '16 at 19:24
  • @Rarara221 check my edits. – heemayl Oct 14 '16 at 19:27
  • Thank you very much that done the trick but I have ran into another problem. The files/folders don't exactly match the add.txt. Some files letter case is wrong (check the top of my post) – Rarara221 Oct 14 '16 at 19:39
  • Also this doesn't add a empty line after the ;; on the first line – Rarara221 Oct 14 '16 at 19:46
  • Not a empty line sorry, just doesn't go down to the next line afterwards. Look at my example, the output currently is ;;%100 – Rarara221 Oct 14 '16 at 19:47
0

With cat:

cat <( echo ";;" ) /path/to/inputfile <( echo ";;" )

If you have a list of files, you can use:

while read file; do
   scratchfile=$(mktemp)
   cat <( echo ";;" ) "$file" <( echo ";;" ) > $scratchfile
   mv -f "$scratchfile" "$file"
done < /path/to/file.list

If you have a command (e. g. find) providing the list, you can use:

IFS="\n"
for file in $( /some/command ); do
   scratchfile=$(mktemp)
   cat <( echo ";;" ) "$file" <( echo ";;" ) > $scratchfile
   mv -f "$scratchfile" "$file"
done
DopeGhoti
  • 76,081
  • Thanks for the reply. That middle command just pasted the changes on screen but did not apply them to each file. – Rarara221 Oct 14 '16 at 20:29
  • I will revise to amend the files. Be aware then that repeated testing will result in a lot of extra semicolons. – DopeGhoti Oct 14 '16 at 20:42