Home > Mobile >  How to remove line breaks and add text instead of them using Batch
How to remove line breaks and add text instead of them using Batch

Time:09-08

i have little problem with uploading some id in textfile to website. File which I exported from system looks like this:

5436
61424
15515

I want to change text to this using batch:

5436;61424;15515

CodePudding user response:

Just read each line and append it to the output file without a linefeed.
In batch, to write a string without a linefeed, you need a little trick:

@echo off
del out.txt
for /f "delims=" %%a in (t.txt) do (
  if exist out.txt (
    <nul set /p "=;%%a" >>out.txt
  ) else (
    <nul set /p "=%%a" >out.txt
  )
)

type out.txt

A bit easier if you can live with a trailing ;:

@echo off
(for /f "delims=" %%a in (t.txt) do <nul set /p "=%%a;") > out.txt
type out.txt

CodePudding user response:

I would do it by writting this simple perl script:

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
     my $inputPath;
     my $outputPath;
    #my $inputPath  = '/Users/myuser/Desktop/a.txt';
    #my $outputPath = '/Users/myuser/Desktop/a_result.txt';
    
    if ($inputPath eq "") {
      print "Enter the full path of your input file: ";
      $inputPath = <STDIN>;
      chomp $inputPath;
    }
    if ($outputPath eq "") {
      print "Enter the full path of your input file: ";
      $outputPath = <STDIN>;
      chomp $outputPath;
    }
    
    open my $info, $inputPath or die "Could not open $inputPath: $!";
    
    open FH, '>', $outputPath or die "Could not open $outputPath : $!";
    
    my $oneline;
    while( my $line = <$info>)  {   
      chomp $line;
      $oneline = $oneline . $line . ";";
    }
    print FH "$oneline";
    close $info;

or print this instead (if you want to omit the last semicolon):

    my $onelineLength = length($oneline);
    $oneline = substr $oneline, 0, ($onelineLength - 1);
    print FH "$oneline";
    
  • Related