1

I am having 5 types of files in same dirctory.then i want to split all the files in the same directory and i need to give the splitted file name as dynamic(original file name + additional extexnsion). File names like(K_MOSTLY*,L_MOSTLY*,M_MOSTLY*,IPBAR*).But i was able to split first matching files only not split second matching files.

for entry in /path/ ;
do
  split -b 700K K_MOSTLY* -d --additional-suffix K_MOSTLY*
  split -b 700K L_MOSTLY* -d --additional-suffix L_MOSTLY*
  split -b 700K M_MOSTLY* -d --additional-suffix M_MOSTLY*
  split -l22000 IPBAR -d --additional-suffix IPBARCODES*
  rm K_MOSTLY*
  rm L_MOSTLY*
  rm M_MOSTLY*
  rm IPBARCODES*
done
rush
  • 27,055
  • 7
  • 87
  • 112
  • What is your intention with `K_MOSTLY*`? Do you want that as the literal suffix on your files? Also, for the `split` command, options come first, then the filename, and last an optional prefix. See the `split` manual. – Kusalananda Jun 24 '19 at 10:03
  • i want to split the all the files in same folder. Filenames like "K_MOSTLY*.*" ..but i could't split the next files in the folder. – logeshwaran Jun 24 '19 at 10:11
  • 2
    I lack an example of what files you have, what you are expecting to get, and what is actually happening. – Kusalananda Jun 24 '19 at 10:12
  • check here https://unix.stackexchange.com/questions/32626/split-a-file-by-line-and-have-control-over-resulting-files-extension – Kamaraj Jun 24 '19 at 10:16
  • I am having 5 types of files in same dirctory.then i want to split all the files in the same directory and i need to give the splitted file name as dynamic(original file name + additional extexnsion). – logeshwaran Jun 24 '19 at 10:17
  • I am having 5 types of files in same dirctory.then i want to split all the files in the same directory and i need to give the splitted file name as dynamic(original file name + additional extexnsion). File names like(K_MOSTLY*,L_MOSTLY*,M_MOSTLY*,IPBAR*).But i was able to split first matching files only not split second matching files. – logeshwaran Jun 24 '19 at 10:25
  • this is i have used scripts:for entry in /path/ ; do split -b 700K K_MOSTLY* -d --additional-suffix K_MOSTLY* split -b 700K L_MOSTLY* -d --additional-suffix L_MOSTLY* split -b 700K M_MOSTLY* -d --additional-suffix M_MOSTLY* split -l22000 IPBAR -d --additional-suffix IPBARCODES* rm K_MOSTLY* rm L_MOSTLY* rm M_MOSTLY* rm IPBARCODES* done – logeshwaran Jun 24 '19 at 10:25
  • how to do above one anyone pls help. – logeshwaran Jun 24 '19 at 10:51
  • @steeldriver is it k? – logeshwaran Jun 24 '19 at 12:32
  • @logeshwaran yes, thank you – steeldriver Jun 24 '19 at 12:48
  • @Kusalananda...did you have any idea bro? – logeshwaran Jun 24 '19 at 12:49

1 Answers1

0

As @Kusalananda says, it's all in the man pages and your syntax is wrong.

That said, your question isn't clear but something along these line should do if you modify the prefix (${f%.*} and IPBAR) and suffix (.${f##*.}) to suit. Be careful of keeping the path intact in the destination filename.

p="./split/"
for f in ${p}[K-M]_MOSTLY* ${p}IPBARCODES*; do
  case "$f" in 
    *IPBARCODES*)
      split -l 2200 -d "$f" "${p}IPBAR" --additional-suffix=".${f##*.}"
      ;; 
    *[K-M]_MOSTLY*)
      split -b 700 -d "$f" "${f%.*}" --additional-suffix=".${f##*.}"
    ;;
  esac
  rm "$f"
done;

This takes

Banana.xml
IPBARCODES.xml
K_MOSTLY.xml
L_MOSTLY.xml
M_MOSTLY.xml

and gives

Banana.xml
IPBAR00.xml
IPBAR01.xml
K_MOSTLY00.xml
K_MOSTLY01.xml
L_MOSTLY00.xml
L_MOSTLY01.xml
M_MOSTLY00.xml
M_MOSTLY01.xml
bu5hman
  • 4,663
  • 2
  • 14
  • 29
  • @Kusalananda i have one more doubt. – logeshwaran Jun 26 '19 at 08:30
  • @Kusalananda why the second new.source*.unl.Z file was not splitting? – logeshwaran Jun 26 '19 at 08:33
  • @Kusalananda this is my scripts:cd /home/kron path="/home/kron/" for var1 in "$path"T_KI* do split -b 700K $var1 --additional-suffix="$(basename $var1)" done for var2 in "$path"madrid_styles*.unl.Z do if [ -f new.source*.unl.Z ]; then gunzip /home/kron/new.source*.unl.Z split -l11000 new.source*.unl --additional-suffix="$(basename $var2)" else exit 0 fi done – logeshwaran Jun 26 '19 at 08:33
  • cd /home/kron path="/home/kron/" for var1 in "$path"T_KI* do split -b 700K $var1 --additional-suffix="$(basename $var1)" done for var2 in "$path"madrid_styles*.unl.Z do if [ -f new.source*.unl.Z ]; then gunzip /home/kron/new.source*.unl.Z split -l11000 new.source*.unl --additional-suffix="$(basename $var2)" else exit 0 fi done – logeshwaran Jun 26 '19 at 08:33