Home > OS >  I need to convert a large space-separated list of Debian packages into a newline-separated list
I need to convert a large space-separated list of Debian packages into a newline-separated list

Time:01-19

In an Amazon s3 bucket we have Debian packages stored in different folders each folder contains different amount of files.
While calling Debian packages from the s3 bucket(AWS) the packages are separated with spaces. Now I need to convert those space-separated packages into a newline-separated list, i.e. one package file per line. The input line doesn't contain an equal amount of spaces.
Each directory contains a different number of Debian packages and at last after converting packages into line-by-line will store all packages(of the different folders) in one file.

input:

package1.deb  package2.deb     pacakge3.deb        pacakge4.deb package5.deb 

output:

package1.deb  
package2.deb  
package3.deb  
pacakge4.deb

This is the current attempt for a function running in the background for different folders of s3 bucket

function convertSpaceToNewLine(){
for line in filename; do
   cat $line| grep '.deb$'|tr [:space:] \\t | sed 's/\t\t*/\n/g' >> folder/newfile
done
}

I have tried many commands like truncate, awk, xargs -n 1, and sed. Thanks

CodePudding user response:

I think using sed to replace spaces with newline would be sufficient.

$ echo package1.deb    package2.deb    pacakge3.deb        pacakge4.deb   package5.deb | sed 's/ /\n/g' 

package1.deb
package2.deb
pacakge3.deb
pacakge4.deb
package5.deb

CodePudding user response:

A simple approach for outputting each space-delimited string separately would be:

awk -v OFS='\n' '{$1=$1}1' file.txt

Now, what you want to do with this output is unknown, and it's impossible to guess it with your code and sample input.

  • Related