- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- how to split file and give prefix name
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-25-2010 12:34 AM
05-25-2010 12:34 AM
I wanna ask about shell scripting.
I have file move_data.sql and it has 600 line command, how to split that file per 100 line and I expect result is, move_data1.sql, move_data2.sql, etc... so I can run it at the same time.
I have try with split, but it always give default prefix aa, ab, etc after file name. please give me an enlightenment guys..
thanks.
Solved! Go to Solution.
- Tags:
- split
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-25-2010 01:00 AM
05-25-2010 01:00 AM
Re: how to split file and give prefix name
but i support cut and paste command can help you.
BR,
Kapil+
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-25-2010 01:20 AM
05-25-2010 01:20 AM
Re: how to split file and give prefix name
Or you can just use a shell while read loop:
(( limit = 100 ))
(( n = 1 ))
(( count = 0 ))
file="move_data$n.sql"
while read line; do
echo "$line" >> $file
(( count += 1 ))
if (( count >= limit )); then
(( count = 0 ))
(( n += 1 ))
file="move_data$n.sql"
fi
done < move_data.sql
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-25-2010 01:37 AM
05-25-2010 01:37 AM
Re: how to split file and give prefix name
man csplit
csplit -k -f move_data.sql move_data.sql 100 "{100}"
rgs,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-25-2010 02:45 AM
05-25-2010 02:45 AM
Re: how to split file and give prefix name
Remember csplit will result the first file to 99 , and it will add 00 suffix, after the extention i.e it will result move_data.sql00 , move_Data.sql01 ...and so on.
The best way perhaps , and perfect with the 600 line command file:
# cat move_data.sql | sed -n '1,100p' > move_data.1.sql
# cat move_data.sql | sed -n '101,200p' > move_data.2.sql
# cat move_data.sql | sed -n '201,300p' > move_data.3.sql
# cat move_data.sql | sed -n '301,400p' > move_data.4.sql
# cat move_data.sql | sed -n '401,400p' > move_data.5.sql
# cat move_data.sql | sed -n '401,500p' > move_data.5.sql
# cat move_data.sql | sed -n '501,600p' > move_data.6.sql
# wc -l *.sql
100 move_data.1.sql
100 move_data.2.sql
100 move_data.3.sql
100 move_data.4.sql
100 move_data.5.sql
100 move_data.6.sql
600 move_data.sql
-----------------------------
Cheers, Have Fun!,
Raj.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-25-2010 03:50 AM
05-25-2010 03:50 AM
Solution>I have file move_data.sql and it has 600 line command, how to split that file per 100 line and I expect result is, move_data1.sql, move_data2.sql, etc
The best way , you can do as below:
#-------------------------------------------
j=1 ; for i in 1 2 3 4 5 6
do
k=`expr $i \* 100` ; cat move_data.sql | sed -n "$j,$k"'p' > move_data$i.sql
j=`expr $j + 100` ; ls -l move_data$i.sql
done
#------------------------------------------
-rw-r--r-- 1 root root 4533 May 26 20:10 move_data1.sql
-rw-r--r-- 1 root root 4518 May 26 20:10 move_data2.sql
-rw-r--r-- 1 root root 4649 May 26 20:10 move_data3.sql
-rw-r--r-- 1 root root 4533 May 26 20:10 move_data4.sql
-rw-r--r-- 1 root root 4518 May 26 20:10 move_data5.sql
-rw-r--r-- 1 root root 4234 May 26 20:10 move_data6.sql
# wc -l move_data?.sql
100 move_data1.sql
100 move_data2.sql
100 move_data3.sql
100 move_data4.sql
100 move_data5.sql
100 move_data6.sql
600 total
#
Enjoy, Have fun!,
Raj.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-25-2010 04:07 AM
05-25-2010 04:07 AM
Re: how to split file and give prefix name
> Raj: The best way , you can do as below:
#-------------------------------------------
j=1 ; for i in 1 2 3 4 5 6
do
k=`expr $i \* 100` ; cat move_data.sql | sed -n "$j,$k"'p' > move_data$i.sql
j=`expr $j + 100` ; ls -l move_data$i.sql
done
#------------------------------------------
This is hardly the "best" from a performance standpoint.
First, you don't need the extra 'cat' process since 'sed' can read thusly:
# sed -n "$j,$k"'p' move_data.sql > move_data$i.sql
# k=$(( i * 100 ))
# j=$(( j + 100 ))
Second, the use of 'expr' to do arithmetic that the shell could do (as Dennis showed) is a waste of another, very slow process.
Third, if you were doing repetitive arithmetic, using 'typeset -i
TMTOWTDI
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-25-2010 05:15 AM
05-25-2010 05:15 AM
Re: how to split file and give prefix name
James,
Thanks! for pointing out the draw back of the code, but I think for the above problem job done is more important than the performance stand point, .
I checked for a 5000 line to split with the shell arithmetic, to 50 file it takes 1.24 sec for a single cpu system., with typeset -i set , it takes 1.22sec , where as the split -l command splits in real quick (real 0m0.15s ) (6 times faster, that makes sense. However the desired output cannot be achieved without compromising little time in this case to process it in the loop seems to be. Thank you ..TMTOWTDI agree..
Raj.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2010 05:04 AM
05-26-2010 05:04 AM