1

I have a file with 7000 lines. I need to combine the lines 100 at a time to reduce it to 70 lines i.e

line 1
line 2
line 3
...
line 7000

becomes

line 1 line 2 line 3 line 4 ... line 100
line 101 line 102 line 103 line 104 ... line 200
...
line 6901 line 6902 line 6903 line 6904 ... line 7000

I know I can do this using code. Is there a way to do this on command line?

4
  • I assume you mean "to reduce it to 70 files i.e" instead of "to reduce it to 70 lines i.e" :) Apr 27, 2015 at 18:42
  • Ah, my eye catches the "I know I can do this using code" just now, is this not the type of answer you are looking for? Let me know and I'll remove it if so. Apr 27, 2015 at 18:48
  • @JacobVlijm I was just commenting on your answer. Perhaps you could leave it as an example of how to do it via code.
    – topher
    Apr 27, 2015 at 20:01
  • Ah, I see, just undeleted it, please add the comment to the answer. Apr 27, 2015 at 20:02

2 Answers 2

2

Try xargs:

xargs -L 100 < file

Test:

for i in {1..7000}; do echo "line $i"; done | xargs -L 100
2
  • 1
    Excellent. Just a heads up, my file contained quotes so xargs -0 -L 100 -d "\n" < file worked for me.
    – topher
    Apr 27, 2015 at 19:48
  • Yes, you can omit echo. I've updated my answer.
    – Cyrus
    Apr 28, 2015 at 5:39
1

I would do something like this:

split -l 100 --numeric-suffixes--additional-suffix=.tmp The_Big_File fragment_  
for f in fragment_[0-9][0-9].tmp ; do  
    tr "\n" " " <$f  
    echo  
    rm $f
done
1
  • Interesting approach. I'll go with Cyrus' answer though.
    – topher
    Apr 27, 2015 at 19:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .