⑴ linux下shell 如何將文件夾下的所有txt文件輸出到一個txt文件下去
假如在當前面目錄下有1.txt和2.txt,現在我們把它輸出到3.txt,具體為:
#!/bin/bash
echo "start file .."
find ./ -name "*.txt" | xargs cat > 3.txt
echo "done !"
你那個出問題應該是因為find在查找的時候沒有加引號還有後面再查找的時候要使用管道命令啊
⑵ shell 遍歷txt文件,取變數
#!/bin/bash
whilereadline
do
eval"$line"2>/dev/null
done<a.txt
echo$b
⑶ shell腳本如何實現該功能一個文件1.txt內容如下aa bb cc dd 50%
⑷ 通過shell循環逐行讀取兩個txt文件,將兩個不同文建內容拼成一個新文件txt
可以試試cstdiofile類
使用cstdiofile的readstring方法可以一次讀取一行,如果前面無用數據的長度是固定的,讀取一行後可以再讀取第i個字元後的字元串,就可以獲得你所需要的數據了,或者使用afxextractsubstring來獲得用空格分隔的字元串的某個位置的數據
讀取多個文件的話可以用一個循環來讀取,讀取前先用getstatus方法來判斷文件是否存在。
⑸ bash shell 腳本如何將字元串寫入到文件中
剛學完新書《實用Linux Shell編程》,寫的不錯,容易懂,網上和書店都有賣。
現在版試著回答,僅供參考,謝謝。
1)先權保證文件內容正確:
$ cat 1.txt
MYNAME=Liu_Dehua
$ cat 2.txt
name Liang_Chaowei
2)文件1文件2隻有唯一字元串,所以情況相對簡單。腳本寫好了,別忘了給腳本增加執行許可權
$ cat build.sh
#!/bin/bash
str1=$(cat 1.txt)
echo ${str1%%=*}=$(cat 2.txt | awk '{print $2}') | tee 1_new.txt
3)運行腳本:
$ ./build.sh
MYNAME=Liang_Chaowei
然後看看新產生的文件,Liang_Chaowei替換了文件1中的Liu_Dehua
$ cat 1_new.txt
MYNAME=Liang_Chaowei
以上在都上機通過了。僅供參考,謝謝。
⑹ bash根據提供的文件名文本1.txt,查找指定目錄下data/的該文件,找到後復制到指定文件夾new_data/
它說你沒寫目標,
用find的話,可以加參數 -exec cp { } ~/Documents/new_data/ \;
⑺ 求shell腳本,刪除一個txt文檔中包含另外一個txt文檔的行。
#!/bin/bash
#
# assume /tmp/a.txt is the file for keywords
# /tmp/b.txt is the file to be operated
awk '
BEGIN{
i=0
while(getline < "/tmp/a.txt" > 0) {
keyword[i++] = $0
}
keysize=i
}
{
flag=0
for(i=0;i<keysize;i++)
if($0 ~ keyword[i])
flag=1
if(flag==0)
print
}' /tmp/b.txt > /tmp/b.txt.tmp
# replace /tmp/b.txt with modified /tmp/b.txt.tmp
mv /tmp/b.txt.tmp /tmp/b.txt
把上述代碼保存為腳本。調試:
debian:~/prog/shell# cat /tmp/a.txt
abc def
def
kllmn
delete
debian:~/prog/shell# cat /tmp/b.txt
#!/bin/bash
delete
kllmn
執行腳本後:
debian:~/prog/shell# cat /tmp/b.txt
#!/bin/bash
⑻ 在linux的bash,怎樣把文件(a.txt)的內容讀到一個變數b里
$ b=`cat a.txt`
`是TAB鍵上那個
⑼ Bash怎麼逐行讀取一個文件
對於 bash、ksh、 zsh 和其他的 shells 語法如下
while read -r line; do COMMAND; done < input.file
通過 -r 選項傳遞給 read 命令以防止阻止解釋其版中的反斜杠轉義符權。
在 read 命令之前添加 IFS= 選項,來防止首尾的空白字元被去掉。
while IFS= read -r line; do COMMAND_on $line; done < input.file
這是更適合人類閱讀的語法:
#!/bin/bashinput="/path/to/txt/file"while IFS= read -r vardo echo "$var"done < "$input"
⑽ linux shell 如何把txt文本中每一行提取出來賦值給一變數,再輸出這一變數
使用cat的話可以這樣:
cat 123.txt | while read line
do
echo "$line"
done
123.txt的每行讀取到變數line中並輸出。