[SOLVED] use sed to break up a csv file into several smaller files
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Ubuntu, Fedora, CEntOS, Linux Mint, PCLinux, AntiX, others
Posts: 12
Rep:
use sed to break up a csv file into several smaller files
I have a large csv file that I want to break into smaller files
There is a title block, that does not share the index labeling of the rest of the file.
There is a labels line, that I want to separate out so I can call it for reports of each individual row in the content section, and there are 2 lines on the end that also do not share the indexing of the content section.
I have gotten this far, and the first 2 sections create their own file properly, but sed does not like variables at all, and it is failing to expand them.
echo "enter input file"; read infile
ct=`wc -l $infile` # I thought I could find out how many
# lines the file had and do an "end minus" to
# get the entire content section and the last 2 lines.
echo $ct # This didn't work, of course.
sed -n '1,7p' $infile > $infile"_title.csv"
sed -n '8p' $infile > $infile"_labels.csv"
sed -n '9,$ct-3p' $infile > $infile"_content.csv"
sed -n '$ct-1,$ctp' $infile > $infile"_endblock.csv"
I have a large csv file that I want to break into smaller files
There is a title block, that does not share the index labeling of the rest of the file.
There is a labels line, that I want to separate out so I can call it for reports of each individual row in the content section, and there are 2 lines on the end that also do not share the indexing of the content section.
I have gotten this far, and the first 2 sections create their own file properly, but sed does not like variables at all, and it is failing to expand them.
Code:
echo "enter input file"; read infile
ct=`wc -l $infile` # I thought I could find out how many
# lines the file had and do an "end minus" to
# get the entire content section and the last 2 lines.
echo $ct # This didn't work, of course.
sed -n '1,7p' $infile > $infile"_title.csv"
You are quoting the wrong thing. Quote the variable not the literal:
Code:
sed -n '1,7p' "$infile" > "$infile"_title.csv
Quote:
Code:
sed -n '8p' $infile > $infile"_labels.csv"
sed -n '9,$ct-3p' $infile > $infile"_content.csv"
sed -n '$ct-1,$ctp' $infile > $infile"_endblock.csv"
Variables inside single quotes are not expanded; use double quotes instead:
Code:
sed -n "$ct-1,$ctp" "$infile" > "$infile"_endblock.csv
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.