Adding a Header into Multiple .Txt Files

How to import multiple txt files in R and add new header?

Try this:

txt_files_ls <- paste(mypath, list.files(path = mypath, pattern = "*.txt"), sep = "/")
txt_files_df_list <- vector("list", length(txt_files_ls))
txt_files_df_list <- lapply(txt_files_ls,
function(x){data.frame(read.table(file = x, header = F,
sep ="\t",colnames(x)))})
combined_df <- setNames(do.call("rbind", txt_files_df_list),
c("INSUR", "POLICY","STREET","STREETPRED","STREETNAME",
"STREETTYPE","STREETPOSTD","STREETADD2","CITY","STATE",
"ZIP","ZIP4","EFFDATE","POLTYPE","PREM","FILL", "BVAMOUNT",
"Full","COUNT"))

How to add text from a file as a header into multiple files which are in separate folder?

Try this (adapt file names and paths accordingly):

for %%F in ("C:\...\*.txt") do (
> "%%~F.tmp" type "Header.txt"
>> "%%~F.tmp" type "%%~F"
move /Y "%%~F.tmp" "%%~F"
)

This combines the content of Header.txt and the processed text file in a temporary file (for instance, the text file is Body.txt, so the temporary file is Body.txt.tmp), then it moves the temproary file over the original text file, hence it get replaced. Note that the heading in the file Header.txt must be terminated with a line-break; otherwise, the heading and the first line of the text file are combined into a single line of text.


In order to check the files whether the already have been inserted the header to, you could redirect their first lines into a variable and compare them with the first line of the header file, like this:

< "Header.txt" set /P HEAD=""
for %%F in ("C:\...\*.txt") do (
< "%%~F" set /P LINE=""
setlocal EnableDelayedExpansion
if not "!LINE!"=="!HEAD!" (
endlocal
> "%%~F.tmp" type "Header.txt"
>> "%%~F.tmp" type "%%~F"
move /Y "%%~F.tmp" "%%~F"
) else endlocal
)

Every single file is checked individually here. For this you need delayed expansion as you are writing to and reading from the same variable within a block of code, that is the for loop.

The best way in Unix to add a header to multiple files in a directory?

Don't use -i. It confuses things when you get interrupted. Instead, use

mkdir -p ../output-dir
for file in *.txt; do
sed '1ichr\tpos\tref\talt\treffrq\tinfo\trs\tpval\teffalt\tgene' "$file" > ../output-dir/"$file"
done

When you're done, you can rename the directories if you wish. This doesn't address the connection issue (ThoriumBR's suggestion of nohup is good for that), but when it happens you can recover state more easily.

adding html tags to multiple text files concatenated into one with a batch file

You could use a for loop to iterate through your (HMTL?) files Text*.txt and do the redirection (>) in its body, like this (see the explanatory remarks):

rem // `for` loop to walk through the files:
for %%F in ("Text*.txt") do (
rem /* one redirection to output file, so output file is opened once;
rem the new files will have `_New` appended to their names: */
> "%%~dpnF_New%%~xF" (
type "header.txt" & rem // (write header to file)
type "%%~fF" & rem // (write original file content)
type "footer.txt" & rem // (write footer to file)
)
)

Adding header to .txt file - doesn't iterate through list of files

stock_files = sorted(glob(raw_path + '/*.sccloud.xxx.com_node.log'))

After this line stock_files is a list (glob returns a list).

for line in fileinput.input([stock_files], inplace=True):

In this line you pass [stock_files] which is a list within a list. Removing the brackets solves the error.

Add text to multiple .txt files

Here is a complete batch code based for this task.

The header and footer file can be configured at top of the batch file. The batch file automatically creates and deletes the header and footer files if not existing already on running the batch file.

All *.txt files in current directory are merged together with the header and footer file to *.html files using command COPY with three source files being specified with + operator. An existing *.html file in current directory with same name as a *.txt file is overwritten except read-only attribute is set on existing HTML file or current NTFS permissions or file sharing access permissions do not grant overwriting existing HTML file.

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "HeaderFile=%TEMP%\Header.tmp"
set "FooterFile=%TEMP%\Footer.tmp"
set "DeleteHeaderFile="
set "DeleteFooterFile="

if not exist "%HeaderFile%" (
set "DeleteHeaderFile=1"
echo ^<HTML^>^<HEAD^>^<META content="IE=5.0000" http-equiv="X-UA-Compatible"^>
echo ^<META content="text/html; charset=windows-1252" http-equiv=Content-Type^>
echo ^<META name=GENERATOR content="MSHTML 11.00.10586.1045"^>^</HEAD^>
echo ^<BODY^>^<PRE^>
) >"%HeaderFile%"

if not exist "%FooterFile%" (
set "DeleteFooterFile=1"
echo ^</PRE^>^</BODY^>^</HTML^>>"%FooterFile%"
)

for %%I in (*.txt) do copy /B "%HeaderFile%"+"%%I"+"%FooterFile%" "%%~nI.html" >nul

if defined DeleteHeaderFile del "%HeaderFile%"
if defined DeleteFooterFile del "%FooterFile%"
endlocal

The COPY option /B for interpreting the files as binary files prevents appending control character substitute at end of generated HTML file.

Please note that FOR ignores *.txt files with hidden attribute set.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • copy /?
  • del /?
  • echo /?
  • endlocal /?
  • for /?
  • set /?
  • setlocal /?

Read also the Microsoft article about Using Command Redirection Operators.



Related Topics



Leave a reply



Submit