How to remove newline character in bash script?

I have a little problem with a small script. There are many entries in the text file, for example:

permission-project1-admin
permission-project2-admin
....

      

The script looks like this (really, it's terrible, but still helps me):

#!/bin/bash
for i in $(cat adminpermission.txt); do
    permission=$(echo $i | cut -f1)

printf "dn:$permission,ou=groups,dc=domain,dc=com \n"
printf "objectclass: groupOfUniqueNames \n"
printf "objectclass: top \n"
printf "description: \n"
printf "cn:$permission \n\n"
done

      

The result looks great, but since there is a newline at the end of the text file, the first printf line is split across two lines:

dn:permission-project1-admin
,ou=groups,dc=domain,dc=com
objectclass: groupOfUniqueNames
objectclass: top
description:
cn:permission-project1-admin

      

My question is, how can I eliminate the newline character between the first two lines?

+3
linux bash


source to share


3 answers


Get it right first.

while read permission rest
do
  ...
done < adminpermission.txt

      



In addition, heredocs.

+3


source to share


Try:



$(echo $i | tr -d "\n" | cut -f1)

      

+2


source to share


Have you checked if your code will return adminpermission.txt

to DOS? Your code will cut lines, but depending on how you view the output, carriage returns might break the lines you describe.

You may try

mv adminpermission.txt backup.txt
tr -d '\r' < backup.txt > adminpermission.txt 

      

to convert to UNIX EOL and then run your script again.

0


source to share







All Articles
Loading...
X
Show
Funny
Dev
Pics