Shell 从一个文本文件中提取数据
抱歉造成混乱,我需要一个脚本,我的要求是如果我是 具有包含标题、记录和尾部的文本文件 例如:-Test1.txt(输入文件)Shell 从一个文本文件中提取数据,shell,unix,Shell,Unix,抱歉造成混乱,我需要一个脚本,我的要求是如果我是 具有包含标题、记录和尾部的文本文件 例如:-Test1.txt(输入文件) 例如 我有一个文本文件 输入文件 我需要编写一个unixshell脚本,从中提取特定的列数据 基于它们的位置输入文件,因为我没有分隔符,甚至没有 这里我必须提取一些列(根据它们的大小随机选择) 位置)并将其保存到文本文件中 例如,假设我需要一个文本文件,可以从位置提取数据 1-5,6-8,9,10-12如图所示,不应包含头部和尾部 normally i have used
例如
我有一个文本文件 输入文件 我需要编写一个unixshell脚本,从中提取特定的列数据 基于它们的位置输入文件,因为我没有分隔符,甚至没有 这里我必须提取一些列(根据它们的大小随机选择) 位置)并将其保存到文本文件中 例如,假设我需要一个文本文件,可以从位置提取数据 1-5,6-8,9,10-12如图所示,不应包含头部和尾部
normally i have used this script to
#Create as same as the input file
cat Test1.txt>tmp.txt
#here i will delete the header and tail from the tmp.txt file
sed '1d,$d' tmp.txt
#now i will extract the data based upon the
cut 1-5,6-8,9,10-12 Test1.txt>Test2.txt
O/p将是这样的Test2.txt
---------
rohitroshank
rohitroshank
rohitroshank
rohitroshank 现在,我的第一个输出文件已准备好Test2.txt 我的第二个要求 现在输出与第一个输出文件相同,但在这里我可以选择一些不同的列,但它是
应包含带有标题、记录和尾部的数据
例如: 打印第一行,这是标题,之后我有记录和之后 尾巴
Test3.txt(output file)
--------------------------
#to print the head
head -1 tmp.txt>Test3.txt
#now i will pick specific columns based upon my positions & append it to test3.txt
cut 13-15,16-19,20 tmp.txt>>Test3.txt
#print and append it to Test3.txt file
tail -1 tmp.txt>>Test.txt
输出Test3.txt
2013101000490398938avuriM26
avuriM26
avuriM26
avuriM26
avuriM26
avuriM26
avuriM26
20131010000405 到目前为止,我的要求已完成,但有没有其他简单的方法来实现这一点 输出。如果是,请与我共享脚本,以便对我有用
And but also now i am stuck with a problem i.e
extracting specific columns data using CUT command.see any text file
each line will contain 1024 characters but i have 4093 characters so how would i
approach this requirement rather than doing it using CUT.
Is there any other way please suggest me. If are having any queries regarding my
requirement comment it here
假设脚本文件名为
special_copy.sh
,并向其传递3个参数,分别为test1.txt
、test2.txt
和test3.txt
,请将以下内容粘贴到sh文件:
head -n -1 $1 | tail -n +2 > $2
cp $1 $3
然后像sh special\u copy.sh test1.txt test2.txt test3.txt那样调用它
#!/bin/bash
tail -n +2 test1.txt | head -n -1 > test2.txt
cp test1.txt test2.txt
exit 0
基本上,它将读取test1.txt
,将除第一行和最后一行之外的所有行复制到test2.txt
,只需复制名为test3.txt
的文件,但我每次都需要运行脚本,是否有其他方法可以从一个脚本运行所有进程我在这里感到困惑。创建这两个文件只需要两个步骤,我已经为您提供了创建shell脚本文件并使用此文件的步骤。如果这不是你想要的,请发布完整的问题。你的问题有点让人困惑。我无法理解你的意思。重复你的上一个问题,用任何脚本编写上面的代码,然后运行它:sh DataExtract
#!/bin/bash
tail -n +2 test1.txt | head -n -1 > test2.txt
cp test1.txt test2.txt
exit 0