Joining traces of textual content on Linux

[ad_1]

There are number of approaches to be a part of many lines of text and improve delimiters if essential. This write-up exhibits two of the easier ways to do this and points out the commands.

Using the tr command

The tr command is really flexible. It is applied to make several styles of improvements to text information, but it can also flatten numerous traces into just one by replacing newline characters with blanks. It does, having said that, clear away the ultimate newline as perfectly. Take note the $ prompt at the finish of the next line. Which is a clue!

$ tr 'n' ' ' < testfile
This is a file that I can use for testing. $
$ tr 'n' ' ' < testfile> newfile

To fix this challenge, you can insert a newline to the end of the file with an echo command like this:

$ echo "" >> newfile
$ od -bc newfile
0000000 124 150 151 163 040 151 163 040 141 040 146 151 154 145 040 164
          T   h   i   s       i   s       a       f   i   l   e       t
0000020 150 141 164 040 111 040 143 141 156 040 165 163 145 040 146 157
          h   a   t       I       c   a   n       u   s   e       f   o
0000040 162 040 164 145 163 164 151 156 147 056 040 012
          r       t   e   s   t   i   n   g   .      n<=== 

Using the paste command

The paste command was developed solely for the purpose of merging the lines in a file into a single line. So this command is a great choice for flattening multi-line text files. If we start with a file like the one below, the result will look like what is shown at the bottom.

$ cat testfile
This is
a file that
I can
use
for testing.
$ paste -sd ' ' testfile
This is a file that I can use for testing.

Redirect the output to a second file to save the flattened text. It will include a newline at the end, so nothing more will need to be done.

$ paste -sd ' ' testfile> testfile2

The -s (use a one file) and -d (insert a delimiter among just about every of the processed lines) in the command shown previously mentioned will be certain that the joined lines are all separated by blanks. You can, nonetheless, use whichever delimiter is appropriate for the task.

If you increase multiple file names with the paste command, each and every file will be turned into a one line in the output.

$ paste -sd ' ' testfile testfile2
This is a file that I can use for testing.
This is a next file.

To conserve the final result, redirect the output to a third file.

$ paste -sd ' ' testfile testfile2 > newfile
$ cat newfile
This is a file that I can use for screening.
This is a 2nd file.

To transform only the first 3 strains of a file into a solitary line, insert a head command like this.

$ head -3 testfile3 | paste -sd ' '
This is a file that I can

If you want to switch just about every consecutive team of 5 strains in a file into a different line, you have to get the job done a minimal tougher. This script joins 5 strains at a time into one by placing all those traces to a separate file and then flattening it.

#!/bin/bash

bottom=5

echo -n "file> "
read file
len=`wc -l $file | awk 'print $1'`

when [ $bottom -le $len ] do
    head -$base $file | tail -5 > temp
    paste -sd ' ' temp
    base=`expr $base + 5`
carried out

# join any remaining traces
remaining=`expr $len - $bottom + 5`
if [ "$remaining" -gt 0 ] then
    tail -$remaining $file | paste -sd ' '
fi

Here’s an example of jogging it:

$ cat biggerfile
This
is
a
larger
file.
Let's
switch it
into
fewer
traces.
$ joinlines
file> biggerfile
This is a even larger file.
Let's switch it into much less strains.

You could very easily change the selection of strains that are sequentially joined jointly by modifying the script or make it even a lot more flexible by prompting the consumer to enter the quantity of lines to be a part of collectively. This is a script with those modifications:

#!/bin/bash

echo -n "file> "
browse file
len=`wc -l $file | awk 'print $1'`

echo -n "Amount of traces to join> "
read through strains
bottom=$lines

when [ $bottom -le $len ] do
    head -$bottom $file | tail -$traces > temp
    paste -sd ' ' temp
    base=`expr $bottom + $lines`
completed

# be part of any remaining lines
remaining=`expr $len - $bottom + $lines`
if [ "$remaining" -gt 0 ] then
    tail -$remaining $file | paste -sd ' '
fi

Here’s an case in point of managing the script:

$ joinlines2
file> biggerfile
Range of lines to join> 3
This is a
larger file. Let's
turn it into fewer
traces.

Wrap-up

Linux gives rather a couple commands that can merge various traces of text into one. The paste command makes the work surprisingly uncomplicated. In truth, if you count on to use the paste -sd ‘ ‘ command usually, you could make the activity even less complicated by turning it into an alias like this:

$ alias paste="paste -sd ' '"
$ paste testfile
This is a file that I can use for screening.

Copyright © 2023 IDG Communications, Inc.

[ad_2]

Supply link