Thursday, February 9, 2023
HomeNetworkingBecoming a member of strains of textual content on Linux

Becoming a member of strains of textual content on Linux


There are variety of methods to affix a number of strains of textual content and alter delimiters if wanted. This text reveals two of the better methods to do that and explains the instructions.

Utilizing the tr command

The tr command is sort of versatile. It’s used to make many sorts of adjustments to textual content recordsdata, however it will probably additionally flatten a number of strains into one by changing newline characters with blanks. It does, nevertheless, take away the ultimate newline as effectively. Word the $ immediate on the finish of the second line. That is a clue!

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

To repair this downside, you possibly can add a newline to the top 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	<=== 

Utilizing the paste command

The paste command was developed solely for the aim of merging the strains in a file right into a single line. So this command is a superb selection for flattening multi-line textual content recordsdata. If we begin with a file just like the one beneath, the outcome will appear like what’s proven on the backside.

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

Redirect the output to a second file to avoid wasting the flattened textual content. It is going to embrace a newline on the finish, so nothing extra will have to be completed.

$ paste -sd ' ' testfile > testfile2

The -s (use a single file) and -d (add a delimiter between every of the processed strains) within the command proven above will make sure that the joined strains are all separated by blanks. You’ll be able to, nevertheless, use no matter delimiter is correct for the duty.

In the event you add a number of file names with the paste command, every file shall be become a single line within the output.

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

To save lots of the outcome, redirect the output to a 3rd file.

$ paste -sd ' ' testfile testfile2 > newfile
$ cat newfile
It is a file that I can use for testing.
It is a second file.

To show solely the primary three strains of a file right into a single line, add a head command like this.

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

If you wish to flip each consecutive group of 5 strains in a file right into a separate line, you need to work a bit more durable. This script joins 5 strains at a time into one by placing these strains to a separate file after which flattening it.

#!/bin/bash

backside=5

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

whereas [ $bottom -le $len ]; do
    head -$backside $file | tail -5 > temp
    paste -sd ' ' temp
    backside=`expr $backside + 5`
completed

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

Right here’s an instance of operating it:

$ cat biggerfile
This
is
a
larger
file.
Let's
flip it
into
fewer
strains.
$ joinlines
file> biggerfile
It is a larger file.
Let's flip it into fewer strains.

You might simply regulate the variety of strains which might be sequentially joined collectively by modifying the script or make it much more versatile by prompting the person to enter the variety of strains to affix collectively. This is a script with these adjustments:

#!/bin/bash

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

echo -n "Variety of strains to affix> "
learn strains
backside=$strains

whereas [ $bottom -le $len ]; do
    head -$backside $file | tail -$strains > temp
    paste -sd ' ' temp
    backside=`expr $backside + $strains`
completed

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

Right here’s an instance of operating the script:

$ joinlines2
file> biggerfile
Variety of strains to affix> 3
It is a
larger file. Let's
flip it into fewer
strains.

Wrap-up

Linux offers fairly a couple of instructions that may mix a number of strains of textual content into one. The paste command makes the job surprisingly straightforward. Actually, in the event you anticipate to make use of the paste -sd β€˜ β€˜ command typically, you might make the duty even simpler by turning it into an alias like this:

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

Copyright Β© 2023 IDG Communications, Inc.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments