Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I delete a newline character in bash script?

Tags:

linux

bash

I have a little problem with a small script. The textfile has a lot of entries like:

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

The script looks like this (indeed, it is an awful one 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 output looks fine, but because the textfile has a newline character at the end, the first line of printf is devided into two lines like:

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

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

like image 409
user2005580 Avatar asked Dec 10 '25 20:12

user2005580


2 Answers

Do it correctly in the first place.

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

Also, heredocs.

like image 116
Ignacio Vazquez-Abrams Avatar answered Dec 12 '25 17:12

Ignacio Vazquez-Abrams


Try:

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

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!