How to Concatenate Multiple Lines of Output to One Line

How to concatenate multiple lines of output to one line?

Use tr '\n' ' ' to translate all newline characters to spaces:

$ grep pattern file | tr '\n' ' '

Note: grep reads files, cat concatenates files. Don't cat file | grep!

Edit:

tr can only handle single character translations. You could use awk to change the output record separator like:

$ grep pattern file | awk '{print}' ORS='" '

This would transform:

one
two
three

to:

one" two" three" 

How to merge multiple lines into single line but only for block of lines separated by blank line

Even a bit shorter than the version of John1024

awk 'BEGIN { RS=""; ORS="\n\n"}{$1=$1}1'

or

awk -v RS="" -v ORS="\n\n" '{$1=$1}1'

Using RS="" tells awk to use any paragraph as a record (i.e. a block of text separated by blank lines). But it also tells awk that a <newline> is always a field separator in combination with FS. By just redefining the output record separator ORS, we can output everything as you want by telling awk to redefine its record $0 by resetting the first record $1=$1. This has as effect that all field separators defined by FS (the default value here) and the newlines (due to RS="") are replaced by OFS (default a <space;>). Finally we print the record with 1

You can get rid of all the spaces when you additionally set OFS=""

RS The first character of the string value of RS shall be the input record separator; a <newline> by default. If RS contains more than one character, the results are unspecified. If RS is null, then records are separated by sequences consisting of a <newline> plus one or more blank lines, leading or trailing blank lines shall not result in empty records at the beginning or end of the input, and a <newline> shall always be a field separator, no matter what the value of FS is.

source: POSIX awk standard

Combine multiple lines (line break) into one line in python

Set the strip=True argument in get_text() to remove a newline (\n):

summary = article.find('div', class_='entry-content entry clearfix').get_text(strip=True)

Since you have already stripped the whitespace from summary, don't call .strip() when writing to the CSV file, instead, use:

csv_writer.writerow([title, summary])

Output:

Health minister-in fimkhur turin mipui ngen nawn
Sawrkarin May ni 31 thleng total lockdown a pawhsei leh hnuah, health minister Dr. R. Lalthangliana chuan nimin khan mipui hnena ngenna leh thuchah tichhuakin, total lockdown chu kan damkhawchhuahna tur a nih tih hriaa inkhuahkhirhna dan te tha taka zawm chunga fimkhurna ngai pawimawh zel turin mipui a chah.Health minister Dr. Thangtea chuan, kan zavaia kan tanrual a, kan tawrh leh rih hram hram a tul dawn a, chutih rualin, tumah riltam leh chhuanchhama kan awm hi sawrkarin a phal lova, kohhran leh khawtlang hruaitute, Local task Force te nen tangrualin theihtawp kan chhuah zel dawn a ni, a ti a. Hetih rual hian sum lakluhna te a lo kiam tak avangin chhungtinin mahni zawnah theuh inrenchem tum ila, fimkhur takin, chi-ai si lovin awm ila, inlenpawh lo turin leh a tul tawpkhawkah lo chuan pawn chhuak rih lo turin kan inchah nawn leh a, kan duh reng vang pawh ni lovin, nunna chhan nan kan tawrh tlan rih hram hram a ngai a ni, a ti bawk.Total lockdown kar hnih kalpui hnu pawha hri kaiin kian lam a la pan theih loh chungchangah health minister chuan, inkharkhip laiin mipui lam hi kan fimkhur tawk lo deuh em tih zawhna a awm hial a ni, a ti a. “Nikum lama khauh taka bazar-na hmuna social distancing kan zawm ang khan kan zawm ta lo em ni aw? ka ti a, mipuite pawh bazar-ah leh puipunna hmunah duty te hmuh phak loha kan awm hian kan fimkhur tawk lo palh ang tih ka hlau a. Mahni theuh kan pawimawh ber a ni tih hriain kan inkhuahkhirhna dan hi khauh deuh mah se, kan zavaia kan himna tura ruahman a ni tih i hre nawn fo ang u,” tiin mipui a chah a.Hri vanga thi awm thin chu lungchhiatthlak a tih thu sawiin health minister chuan, “Kan state-a thi zat hi a tam tawh viaua a lan laiin hmarchhak state dang te leh India ram ngaihtuah chuan kan dinhmun a la ziaawm hle a. Kan positivity rate a sang kan tih pawh hi test kan neih that vang a ni ve pakhat a, kan test percentage hi 31.49 niin hmarchhak state-ah Arunachal Pradesh tih lohah chuan test tam ber kan ni,” a ti.Vaccine chungchangah, sawrkarin vaccine a chah mek thu leh, Central Ministry lamin inkaihhruaina a siam ang zelin chak taka vaccine pek hna kalpui zel tum a nih thu a sawi a. Mizoramin khawvel ram hrang hrang United Kingdom, Egypt, Ireland, Switzerland, Turkey, China, Taiwan atangtein kan mamawh hmanrua leh khawl chi hrang hrang kan dawng tawh tih sawiin, USA, Spain leh Kuwait atang pawhin tanpuina dawn tur a la awm thu te, World Health Organisation atanga oxygen concentrator 150 dawn a nih thu te pawh a sawi bawk.Minister chuan, ram hruaitu Minister-te leh MLA zawng zawngte an thawkrim hle tih sawiin, “Kan thawhhona a thatna leh mipuite thlawpna avangin he hripui hi kan hneh ngei dawn a ni,” a ti.“Total lockdown puang tura kohhran, Local Council Association, NGO leh VC Association te bakah mi thahnemngaite ngenna a lawmawmin a zahawm ka ti hle a, nitina eizawngte lakah inthlahrunawm viau mahse state dangte pawhin lockdown lo chu kawng dang zawh tur an hre bik meuh lo tih i hre tlang ila. Lockdown chhung hian kan frontline worker te leh healthcare worker ten nasa takin hma an la a, theihtawp an chhuah a ni tih i hriatsak ang u. Kan rorelah sawisel tur leh khamkhawp loh tam tak in hmu thin tih pawh ka hria a, khawvel pum luhchhuahtu hri a ni a, mi zawng zawng min nghawng vek a, rorel thiam a har a, vawiina tha kan tih kha naktuk lawkah a lo tha leh lova, engmah experiment han tih hman a ni si lo, kan rorelah leh kan tawngkam chhuakah te in rilru kan tih nat a awm chuan khawngaihtakin min ngaidam ula, inhriatthiamna nen dawhthei takin indawm tlang ila, thurawn tha leh fing engtiklai pawhin kan dawng thei reng a ni,” health minister chuan a ti a. Pathian venhimna leh a chhanchhuahna bang lova dil turin leh, malsawm tlak ni tura mahni lamin kan tih ve theihte ti ve turin Zoram mipuite a ngen a ni.

How can I combine multiple lines of text into one line in Python with a delimiter to separate them with big files (4gb+)

You can read from input file and write to output file at same time, for example:

current = []

with open('DEFIS.TXT', 'r') as f_in, open('DEFIS-OUT.TXT', 'w') as f_out:
for line in map(str.strip, f_in):
if line.startswith('D1000'):
if current:
print('|'.join(current), file=f_out)
current = []
current.append(line)
#save last chunk (if any):
if current:
print('|'.join(current), file=f_out)

If DEFIS.TXT contains:

D1000
1
2
3
D1000
4
5
6
D1000
7
8
9

Then DEFIS-OUT.TXT after running the script will contain:

D1000|1|2|3
D1000|4|5|6
D1000|7|8|9

Merge multiple lines to single line in a file skipping the header

With perl

$ perl -pe 's/\n// if $. > 1 && !eof' AAB08704.1.fasta 
>gi|1117824|gb|AAB08704.1| ecdysteroid regulated 16 kDa [Manduca sexta]
MLFYITVTVLLVSAQAKFYTDCGSKLATVQSVGVSGWPENARECVLKRNSNVTISIDFSPTTDVSAITTEVHGVIMSLPVPFPCRSPDACKDNGLTCPIKAGVVANYKTTLPVLKSYPKVSVDVKWELKKDEEDLVCILIPARIH
  • s/\n// remove newline

    • if $. > 1 && !eof only if line number is greater than one and not end of file
  • Use perl -i -pe for inplace editing. See Command Switches for documentation on -i, -p and -e


Related Topics



Leave a reply



Submit