Add a Header to a Tab Delimited File

Add a header to a tab delimited file

There isn't a "prepend" operator like the "append" operator >>, but you can write the header to a temp-file, copy your file's contents into the temp-file after that, and move it back:

echo -e "name\tage\tuniversity\tcity" | cat - yourfile > /tmp/out && mv /tmp/out yourfile

How to insert headers into a tab delimited file?

First make sure that delimiter is really what you think. You can check this by opening the file using openoffice or writing a python function to detect the delimiter using regular expressions (re module). Also make sure that lines are ending with "/n" or windows style (additional r).

Header is nothing more than the first line. So open the file in python, read all lines. Prepend the header string (separated by /t for tab delimiter ) to the first line. Write the lines back to the file.
That's it.

add header to tab delimited csv file with python

Can something like this help?

import pandas as pd
my_file = "C:\my_path\my_file.csv"
df = pd.read_csv(my_file, sep='\t', names = ["a", "b", "c", "d", "e", "f"])
df.to_csv(my_file, sep='\t', index = False)

Add column with a header to a tab-delimited text file?

See answer here, a tab delimeted file is like CSV with tab as separator.

How to add a new column to a CSV file using Python?

Add headers and a new column to a tab-delimited file

from http://docs.python.org/2/library/csv.html#csv.DictWriter.writeheader it's clear how to use it - without any additional argumets, i.e.:

 dict_writer.writeheader()

How can I alternate column headers in a tab delimited file?

The Import-CSV and Export-CSV cmdlets have their strengths but this might not be one of them. The latter cmdlet would introduce quoting that might not be in your original file and that might not be desired.

Either way why not just do some text manipulation on the first line! Lets read in the file and and output the first lined, edited, and the remainder of the file. This sample uses a new location but you could easily write it back to the same file.

# Get the full file into a variable
$fullFile = Get-Content "c:\temp\mockdata.csv"

# Parse the first line into a column array
$columns = $fullFile[0].Split("`t")

# Rebuild the header by switching the columns order as desired.
$newHeader = ($columns[1],$columns[0] + ($columns | Select-Object -Skip 2)) -join "`t"

# Write the header back to file then the rest of the data.
$outputPath = "C:\somepath.txt"

$newHeader | Set-Content $outputPath
$fullFile | Select-Object -Skip 1 | Add-Content $outputPath

This also preserves the presence of other columns and their data.

replace column header for multiple tab delimited files

here is one way to do this in sed

$ sed -i'.bak' -r '1s/([^\t]*\t)OLD/\1NEW/' file

if you want to in place change the files, sed is a better alternative. You can do all in a loop

$ for f in files; do sed -i'.bak' -r ... $f; done

awk alternative can be

$ awk -v OFS='\t' 'NR==1 && $2=="OLD"{$2="NEW"}1' file > temp && mv temp file

How to add row name in tab delimited file with header in Linux


awk 'BEGIN {i=0} {print "AM_" i++ "_arp " $0}' <file> | sed 's/AM_0_arp/sim/g'

I'll just add your string of choice with a counter in between to the beginning of each line and then replace the first instance with sim for the header.

Insert a pipe separated header to a large txt file

Try this:

sed '1s/.*/user_id|name|age|transactions\n&/' file.txt


Related Topics



Leave a reply



Submit