Add line to beginning of text file without newline

So I'm trying to import an array of massive tabs txt

(200MB) in R

for statistical analysis. Unfortunately the file format does not match the number of columns / rows. The first line contains the header names for all but the first two columns.

Example:

header3 header4 header5

column1 column2 column3 column4 column5

      

I can restore the file after reading it into R

, but a more elegant way would be to insert header1 header2

into the file. I tried:

cat file_with_missing_headers main_file > new_file

      

The result is a new line in between. Is there a way to suppress the new line?
Or maybe another tool?

+3


source to share


2 answers


Solution 1

$ { echo -n "header1 header2 "; cat file; } >newfile

      

-n

suppresses unwanted newline.

Solution 2



Use sed

:

$ sed '1s/^/header1 header2 /' file >newfile

      

Because of 1

, substitution is performed on the first line (and only the first line) file

. The caret ^

matches the start of the first line. The headers are replaced at the beginning of the line, while the rest of the file remains unchanged.

+5


source


In R, you can ignore the first line and then create a new set of names. For example use

setNames(read.table(fileName, skip = 1), paste0("header", 1:5))

      



will read the file and immediately set the new column names. Note that the default delimiter for read.table

is sep = ""

, so if it's a csv file you need to change it to sep = ","

.

Then, when you're done, you can write a new set of data to the same file with using write.table

, and you don't have to worry about it the next time you use the data.

+2


source







All Articles